版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中相微乳液驱纳米驱油剂提采技术
- 甘南市重点中学2025-2026学年初三下学期第一次联考生物试题试卷含解析
- 2026年公共数据提供单位收益激励与数据质量提升联动机制
- 2026年无线基站天线PCB等传统设备商及车联网卫星导航模组厂受益方向
- 2026年低空目标探测中的多普勒频移应用
- 2026年RTCP功能含义与机床结构参数标定方法
- 人力资源管理师职称评定指南
- 金融分析师面试问题探讨
- 记者招聘面试指南及问题详解
- 新零售平台运营经理面试全解析与技巧
- 抚育林施工方案(3篇)
- 【初中 物理】二力平衡课件-2025-2026学年人教版物理八年级下册
- 【华信咨询】中国智算中心(AIDC)产业发展白皮书(2024年)
- 2026年及未来5年市场数据中国吡咯喹啉醌PQQ行业发展潜力分析及投资战略数据分析研究报告
- 重建祠堂施工方案(3篇)
- 酒店餐厅外包协议书
- 2026年湖南城建职业技术学院单招职业技能测试模拟测试卷附答案
- 燃气管道安装质量控制管理措施
- 2025年公安专业科目考试真题及答案
- 儿童肥胖疾病科普
- 水电预埋施工流程方案
评论
0/150
提交评论