收藏 分享(赏)

java基础教程4.ppt

上传人:无敌 文档编号:974529 上传时间:2018-05-11 格式:PPT 页数:20 大小:359.50KB
下载 相关 举报
java基础教程4.ppt_第1页
第1页 / 共20页
java基础教程4.ppt_第2页
第2页 / 共20页
java基础教程4.ppt_第3页
第3页 / 共20页
java基础教程4.ppt_第4页
第4页 / 共20页
java基础教程4.ppt_第5页
第5页 / 共20页
点击查看更多>>
资源描述

1、Reusing classes,Composition syntax,Simply place object references inside new classes. /SprinklerSystem.javaReference initialization. /Bath.javaAt the point the objects are defined;In the constructor for that class;Right before you actually need to use the object. (lazy initialization)Using instance

2、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 class,When you create an objec

3、t 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 you want to call a base-class co

4、nstructor 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 the class youre building.You can al

5、so 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.javaAlthough the compiler forces you to initialize the base classes

6、, 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.javaFirst perform all the cleanup w

7、ork 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-class version. /Hide.javaOverride,C

8、hoosing 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.javaWhen you inherit, you take an existing class and make a special version of it.The is-a relationship is expressed with inherita

9、nce, 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.javaAlthough its possible to create protected fields, the best approach is to leave the fields private; you should always pres

10、erve your right to change the underlying implementation.,Upcasting,/Wind.javaCasting 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 cannot be changed”There are three places wher

11、e 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 constants must be primitives and are expressed with th

12、e 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 reference is initialized to an object, it can never

13、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.A blank final field inside a class can be differen

14、t 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 met

15、hods: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 class is final, you state that you dont want to inherit from this class or allow anyone else to do so. /Jurassic.javaThe fields of a final class can be final or not.All methods in a final class are implicitly final.,To be continued,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 网络科技 > Java

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报