读TIJ -6 类再生

《Think in java·第 6 章类再生》

读TIJ -1 对象入门 中已经就代码复用性吐槽了,不再反复。

继承关系在结果上(而不是为了代码复用而设计继承关系)为代码复用提供了新的途径。

本章看其文件夹就比較杂,程序猿的内聚思想去哪儿了?合成复用优先——合成、继承,为什么实现继承是不好的……

6.1 合成的语法

解释了一个例程,没有什么好说的。has_a关系。

6.2 继承的语法

【You can think of inheritance, then, as reusing the class.】Is_a。

super.xx( )、扩展

6.2.1 初始化基类  【p81一旦新对象被创建。就在Java heap(堆)中给它分配内存空间,以保存它全部的实例变量的值。这里所指的实例变量不仅包含自己声明的全部实例变量,也包含全部祖先类中声明的全部实例变量(父类的private实例变量也在内)】。所以子类对象包含了一个基类的对象作为“子对象”,能够想象。子对象必须被正确初始化,那么就得调用它(即父类的)构造器。

6.3 合成与继承的结合

A1包括B又是A的子类。

6.3.1 确保正确的清除 这人对清除情有独钟啊。

finally clause

6.3.2 名字的隐藏  父类的方法m1(int)、m1(String)与子类定义m1(double)。简单的overload。既然说道名字的隐藏,就能够谈一下父类的public static m1(int)和子类的public static m1(int)

6.4 究竟选择合成还是继承

【须要将成员对象的属性变为public】Never

6.5 protected

太简单

6.6 累积开发

继承的一个优点是它支持增量开发

6.7 上溯造型Upcasting

这一节比較流畅。
The most important aspect of inheritance is not that it provides methods for the new class. It’s the relationship expressed between the new class and the base class. This relationship can be summarized by saying, “The new class is a type of the existing class.”
6.7.1 何谓“上溯造型”  是由于类图中父类在上,【Casting from a derived type to a base type moves up on the inheritance diagram】。在讲依赖的单向性时。我通常把父类放在以下。


再论合成与继承 不错。

可是不如直接讲LSP。

6.8 final keyword

6.8.1 final 数据  “constant”通常须要static final。final 修饰引用变量,仅保证其引用不变。

Java does not provide a way to make any arbitrary object a constant.—>不变对象。


3. final 形參  方法体中不可以又一次赋值。在普通情况下,【 This feature seems only marginally useful】,可是对于内部类实用。
6.8.2 final 方法  1阻止其它人改写 2效率。
6.8.3 final 类 

6.9 初始化和类装载


就这些,


原文地址:https://www.cnblogs.com/wgwyanfs/p/7237818.html