继承中的私有成员访问问题
// 程序执行结果 // Listing 1输出 Contents of superOb: i and j: 10 20 Contents of subOb: i and j: 7 8 k: 9 Sum of i, j and k in subOb: ijk: 24 // Listing 2 编译错误 // 错误信息j 在类 B 中不可访问因为它在类 A 中被声明为 private。 // 程序无法编译通过。 // Listing 3 输出 Volume of mybox1 is 3000.0 Weight of mybox1 is 34.3 Volume of mybox2 is 24.0Weight of mybox2 is 0.076 // Listing 4 无输出仅为类定义。 // Listing 5 输出 Volume of weightbox is 105.0 Weight of weightbox is 8.37 Volume of plainbox is 105.0 // 注释掉的语句 System.out.println(Weight of plainbox is plainbox.weight); 如果执行会报错因为 plainbox 是 Box 类型引用无法访问子类 BoxWeight 的 weight 成员。 // Listing 6 无输出仅为改进的类定义。 // Listing 7 输出 Volume of mybox1 is 3000.0 Weight of mybox1 is 34.3 Volume of mybox2 is 24.0Weight of mybox2 is 0.076 Volume of mybox3 is -1.0 Weight of mybox3 is -1.0 Volume of myclone is 3000.0 Weight of myclone is 34.3 Volume of mycube is 27.0 Weight of mycube is 2.0 // Listing 8 无输出仅为构造函数代码片段。 // Listing 9 输出 i in superclass: 1 i in subclass: 2 // Listing 10 输出 Volume of shipment1 is 3000.0Weight of shipment1 is 10.0 Shipping cost: $3.41 Volume of shipment2 is 24.0 Weight of shipment2 is 0.76 Shipping cost: $1.28 // Listing 11 输出 Inside As constructor. Inside Bs constructor. Inside Cs constructor. // Listing 12 输出 k: 3 // Listing 13 无输出仅为改进的类定义。 // Listing 14 输出 This is k: 3 i and j: 1 2 // Listing 15 输出 Inside As callme method Inside Bs callme method Inside Cs callme method // Listing 16 输出 Inside Area for Rectangle. Area is 45.0 Inside Area for Triangle. Area is 40.0 Area for Figure is undefined. Area is 0.0 // Listing 17 输出 Bs implementation of callme. This is a concrete method. // Listing 18 输出 Inside Area for Rectangle. Area is 45.0 Inside Area for Triangle. Area is 40.0 // Listing 19 编译错误 // 错误信息meth() 在类 B 中无法被覆盖因为它在类 A 中被声明为 final。 // Listing 20 编译错误 // 错误信息类 B 无法继承类 A因为类 A 被声明为 final。参考来源java中比较对象面积的大小_java面积对象考试题关于java中初始化块、静态方法块与构造方法执行顺序问题探索 Java 领域 Eclipse 的代码分析工具JAVA语言总体讲解与一些代码分析Java中的代码分析工具