@@ -769,20 +769,215 @@ public class Car {
769769
770770当使用继承时,使用一个现有类并开发出它的新版本。通常这意味着使用一个通用类,并为了某个特殊需求将其特殊化。稍微思考下,你就会发现,用一个交通工具对象来组成一部车是毫无意义的——车不包含交通工具,它就是交通工具。这种“是一个”的关系是用继承来表达的,而“有一个“的关系则用组合来表达。
771771
772-
773-
774772<!-- protected -->
775773
776774## protected
777775
776+ 即然你已经接触到继承,关键字 ** protected** 就变得有意义了。在理想世界中,仅靠关键字 ** private** 就足够了。在实际项目中,却经常想把一个事物尽量对外界隐藏,而允许派生类的成员访问。
777+
778+ 关键字 ** protected** 就起这个作用。它表示“就类的用户而言,这是 ** private** 的。但对于任何继承它的子类或在同一包中的类,它是可访问的。”(** protected** 也提供了包访问权限)
779+
780+ 尽管可以创建 ** protected** 属性,但是最好的方式是将属性声明为 ** private** 以一直保留更改底层实现的权利。然后通过 ** protected** 控制类的继承者的访问权限。
781+
782+ ``` java
783+ // reuse/Orc.java
784+ // The protected keyword
785+ class Villain {
786+ private String name;
787+
788+ protected void set (String nm ) {
789+ name = nm;
790+ }
791+
792+ Villain (String name ) {
793+ this . name = name;
794+ }
795+
796+ @Override
797+ public String toString () {
798+ return " I'm a Villain and my name is " + name;
799+ }
800+ }
801+
802+ public class Orc extends Villain {
803+ private int orcNumber;
804+
805+ public Orc (String name , int orcNumber ) {
806+ super (name);
807+ this . orcNumber = orcNumber;
808+ }
809+
810+ public void change (String name , int orcNumber ) {
811+ set(name); // Available because it's protected
812+ this . orcNumber = orcNumber;
813+ }
814+
815+ @Override
816+ public String toString () {
817+ return " Orc " + orcNumber + " : " + super . toString();
818+ }
819+
820+ public static void main (String [] args ) {
821+ Orc orc = new Orc (" Limburger" , 12 );
822+ System . out. println(orc);
823+ orc. change(" Bob" , 19 );
824+ System . out. println(orc);
825+ }
826+ }
827+ ```
828+
829+ 输出:
830+
831+ ```
832+ Orc 12: I'm a Villain and my name is Limburger
833+ Orc 19: I'm a Villain and my name is Bob
834+ ```
835+
836+ ` change() ` 方法可以访问 ` set() ` 方法,因为 ` set() ` 方法是 ** protected** 。注意到,类 ** Orc** 的 ` toString() ` 方法也使用了基类的版本。
837+
778838
779839<!-- Upcasting -->
780840## 向上转型
781841
842+ 继承最重要的方面不是为新类提供方法。它是新类与基类的一种关系。简而言之,这种关系可以表述为“新类是已有类的一种类型”。
843+
844+ 这种描述并非是解释继承的一种花哨方式,这是直接由语言支持的。例如,假设有一个基类 ** Instrument** 代表音乐乐器和一个派生类 ** Wind** 。 因为继承保证了基类的所有方法在派生类中也是可用的,所以任意发送给该基类的消息也能发送给派生类。如果 ** Instrument** 有一个 ` play() ` 方法,那么 ** Wind** 也有该方法。这意味着你可以准确地说 ** Wind** 对象也是一种类型的 ** Instrument** 。下面例子展示了编译器是如何支持这一概念的:
845+
846+ ``` java
847+ // reuse/Wind.java
848+ // Inheritance & upcasting
849+ class Instrument {
850+ public void play () {}
851+
852+ static void tune (Instrument i ) {
853+ // ...
854+ i. play();
855+ }
856+ }
857+
858+ // Wind objects are instruments
859+ // because they have the same interface:
860+ public class Wind extends Instrument {
861+ public static void main (String [] args ) {
862+ Wind flute = new Wind ();
863+ Instrument . tune(flute); // Upcasting
864+ }
865+ }
866+ ```
867+
868+ ` tune() ` 方法接受了一个 ** Instrument** 类型的引用。但是,在 ** Wind** 的 ` main() ` 方法里,` tune() ` 方法却传入了一个 ** Wind** 引用。鉴于 Java 对类型检查十分严格,一个接收一种类型的方法接受了另一种类型看起来很奇怪,除非你意识到 ** Wind** 对象同时也是一个 ** Instrument** 对象,而且 ** Instrument** 的 ` tune ` 方法一定会存在于 ** Wind** 中。在 ` tune() ` 中,代码对 ** Instrument** 和 所有 ** Instrument** 的派生类起作用,这种把 ** Wind** 引用转换为 ** Instrument** 引用的行为称作* 向上转型* 。
869+
870+ 该术语是基于传统的类继承图:图最上面是根,然后向下铺展。(当然你可以以任意方式画你认为有帮助的类图。)于是,** Wind.java** 的类图是:
871+
872+ ![ Wind 类图] ( ../images/1561774164644.png )
873+
874+ 继承图中派生类转型为基类是向上的,所以通常称作* 向上转型* 。因为是从一个更具体的类转化为一个更一般的类,所以向上转型永远是安全的。也就是说,派生类是基类的一个超集。它可能比基类包含更多的方法,但它必须至少具有与基类一样的方法。在向上转型期间,类接口只可能失去方法,不会增加方法。这就是为什么编译器在没有任何明确转型或其他特殊标记的情况下,仍然允许向上转型的原因。
875+
876+ 也可以执行与向上转型相反的向下转型,但是会有问题,对于该问题会放在下一章和“类型信息”一章进行更深入的探讨。
877+
878+ ### 再论组合和继承
879+
880+ 在面向对象编程中,创建和使用代码最有可能的方法是将数据和方法一起打包到类中,然后使用该类的对象。也可以使用已有的类通过组合来创建新类。继承其实不太常用。因此尽管在教授 OOP 的过程中我们多次强调继承,但这并不意味着要尽可能使用它。恰恰相反,尽量少使用它,除非确实使用继承是有帮助的。一种判断使用组合还是继承的最清晰的方法是问一问自己是否需要把新类向上转型为基类。如果必须向上转型,那么继承就是必要的,但如果不需要,则要进一步考虑是否该采用继承。“多态”一章提出了一个使用向上转型的最有力的理由,但是只要记住问一问“我需要向上转型吗?”,就能在这两者中作出较好的选择。
881+
782882<!-- The final Keyword -->
783883
784884## final关键字
785885
886+ 根据上下文环境,Java 的关键字 ** final** 的含义有些微的不同,但通常它指的是“这是不能被改变的”。防止改变有两个原因:设计或效率。因为这两个原因相差很远,所以有可能误用关键字 ** final** 。
887+
888+ 以下几节讨论了可能使用 ** final** 的三个地方:数据、方法和类。
889+
890+ ### final 数据
891+
892+ 许多编程语言都有某种方法告诉编译器有一块数据是恒定不变的。恒定是有用的,如:
893+
894+ 1 . 一个永不改变的编译时常量。
895+ 2 . 一个在运行时初始化就不会改变的值。
896+
897+ 对于编译时常量这种情况,编译器可以把常量带入计算中;也就是说,可以在编译时计算,减少了一些运行时的负担。在 Java 中,这类常量必须是基本类型,而且用关键字 ** final** 修饰。你必须在定义常量的时候进行赋值。
898+
899+ 一个被 ** static** 和 ** final** 同时修饰的属性只会占用一段不能改变的存储空间。
900+
901+ 当用 ** final** 修饰对象引用而非基本类型时,其含义会有一点令人困惑。对于基本类型,** final** 使数值恒定不变,而对于对象引用,** final** 使引用恒定不变。一旦引用被初始化指向了某个对象,它就不能改为指向其他对象。但是,对象本身是可以修改的,Java 没有提供使任何对象恒定不变的方法。(你可以自己编写类达到使对象恒定不变的效果)这一限制同样适用数组,数组也是对象。
902+
903+ 下面例子展示了 ** final** 属性的使用:
904+
905+ ``` java
906+ // reuse/FinalData.java
907+ // The effect of final on fields
908+ import java.util.* ;
909+
910+ class Value {
911+ int i; // package access
912+
913+ Value (int i ) {
914+ this . i = i;
915+ }
916+ }
917+
918+ public class FinalData {
919+ private static Random rand = new Random (47 );
920+ private String id;
921+
922+ public FinalData (String id ) {
923+ this . id = id;
924+ }
925+ // Can be compile-time constants:
926+ private final int valueOne = 9 ;
927+ private static final int VALUE_TWO = 99 ;
928+ // Typical public constant:
929+ public static final int VALUE_THREE = 39 ;
930+ // Cannot be compile-time constants:
931+ private final int i4 = rand. nextInt(20 );
932+ static final int INT_5 = rand. nextInt(20 );
933+ private Value v1 = new Value (11 );
934+ private final Value v2 = new Value (22 );
935+ private static final Value VAL_3 = new Value (33 );
936+ // Arrays:
937+ private final int [] a = {1 , 2 , 3 , 4 , 5 , 6 };
938+
939+ @Override
940+ public String toString () {
941+ return id + " : " + " i4 = " + i4 + " , INT_5 = " + INT_5 ;
942+ }
943+
944+ public static void main (String [] args ) {
945+ FinalData fd1 = new FinalData (" fd1" );
946+ // - fd1.valueOne++; // Error: can't change value
947+ fd1. v2. i++ ; // Object isn't constant
948+ fd1. v1 = new Value (9 ); // OK -- not final
949+ for (int i = 0 ; i < fd1. a. length; i++ ) {
950+ fd1. a[i]++ ; // Object isn't constant
951+ }
952+ // - fd1.v2 = new Value(0); // Error: Can't
953+ // - fd1.VAL_3 = new Value(1); // change reference
954+ // - fd1.a = new int[3];
955+ System . out. println(fd1);
956+ System . out. println(" Creating new FinalData" );
957+ FinalData fd2 = new FinalData (" fd2" );
958+ System . out. println(fd1);
959+ System . out. println(fd2);
960+ }
961+ }
962+ ```
963+
964+ 输出:
965+
966+ ```
967+ fd1: i4 = 15, INT_5 = 18
968+ Creating new FinalData
969+ fd1: i4 = 15, INT_5 = 18
970+ fd2: i4 = 13, INT_5 = 18
971+ ```
972+
973+ 因为 ** valueOne** 和 ** VALUE_TWO** 都是带有编译时值的 ** final** 基本类型,它们都可用作编译时常量,没有多大区别。
974+
975+ ### final 方法
976+
977+
978+
979+ ### final 类
980+
786981
787982<!-- Initialization and Class Loading -->
788983## 类初始化和加载
0 commit comments