JAVA基础教程4.ppt_第1页
JAVA基础教程4.ppt_第2页
JAVA基础教程4.ppt_第3页
JAVA基础教程4.ppt_第4页
JAVA基础教程4.ppt_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Reusing classes,华南理工大学 计算机科学与工程学院 韦佳 ,Composition syntax,Simply place object references inside new classes. /SprinklerSystem.java Reference initialization. /Bath.java At the point the objects are defined; In the constructor for that class; Right before you actually need to use the object. (lazy init

2、ialization) Using instance initialization.,Inheritance syntax,extends keyword. /Detergent.java “this new class is like that old class.” You automatically get all the fields and methods in the base class. super keyword. Refers to the “superclass” that the current class inherits.,Initializing the base

3、 class,When you create an object of the derived class, it contains within it a subobject of the base class. Java automatically inserts calls to the base-class constructor in the derived-class constructor. /Cartoon.java,Constructors with arguments,If there is no default base-class constructor, or if

4、you want to call a base-class constructor that has arguments, you must explicitly write a call to the base-class constructor using the super keyword and the appropriate argument list. /Chess.java,Delegation,Delegation is midway between inheritance and composition. You can place a member object in th

5、e class youre building. You can also expose all the methods from the member object in your new class. /SpaceShipControls.java; SpaceShipDelegation.java,Combining composition and inheritance,It is very common to use composition and inheritance together. /PlaceSetting.java Although the compiler forces

6、 you to initialize the base classes, it doesnt watch over you to make sure that you initialize the member objects, so you must remember to pay attention to that.,Guaranteeing proper cleanup,If you want something cleaned up for a class, you must explicitly write a special method to do it. /CADSystem.

7、java First perform all the cleanup work specific to your class, in the reverse order of creation. Then call the base-class cleanup method.,Name hiding,If a Java base class has a method name thats overloaded several times, redefining that method name in the derived class will not hide any of the base

8、-class version. /Hide.java Override,Choosing composition vs. inheritance,Composition is generally used when you want the functionality of an existing class inside your new class, but not its interface. /Car.java When you inherit, you take an existing class and make a special version of it. The is-a

9、relationship is expressed with inheritance, and the has-a relationship is expressed with composition.,protected,protected: available to anyone who inherits from this class or anyone else in the same package. /Orc.java Although its possible to create protected fields, the best approach is to leave th

10、e fields private; you should always preserve your right to change the underlying implementation.,Upcasting,/Wind.java Casting from a derived type to a base type moves up on the inheritance diagram, so its commonly referred to as upcasting. Upcasting is always safe.,The final keyword,final: “This can

11、not be changed” There are three places where final can be used: data, methods, and classes.,final data,A constant is useful for two reasons: It can be a compile-time constant that wont ever change. It can be a value initialized at run time that you dont want changed. In java, these sorts of constant

12、s must be primitives and are expressed with the final keyword. A value must be given at the time of definition of such a constant.,A field that is both static and final has only one piece of storage that cannot be changed. With an object reference, final makes the reference a constant. Once the refe

13、rence is initialized to an object, it can never be changed to point to another object. /FinalData.java,Blank finals,Java allows the creation of blank finals, which are fields that are declared as final but are not given an initialization value. The blank final must be initialized before it is used.

14、A blank final field inside a class can be different for each object. /BlankFinal.java,final arguments,Java allows you to make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to. /FinalArguments.java,final methods,There are two reasons for final methods: Prevent any inheriting class from changing its meaning. Efficiency. Any private methods in a class are implicitly final. Confusion. /FinalOverridingIllusion.java,final classes,When you say that an entire

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论