1 # //: c08:BigEgg.java
2 # // An inner class cannot be overriden like a method.
3 # // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 //本例的目的:为了测试内部类是否会被重写!!!答案:NO!!!
4 # // www.BruceEckel.com. See copyright notice in CopyRight.txt.
5 # import com.bruceeckel.simpletest.*;
6 #
7 # class Egg {
8 # private Yolk y;
9 # protected class Yolk { //在Egg类中定义了内部类Yolk,并且是受保护的
10 # public Yolk() { System.out.println("Egg.Yolk()"); }
11 # }
12 # public Egg() {
13 # System.out.println("New Egg()");
14 # y = new Yolk();
15 # }
16 # }
17 #
18 # public class BigEgg extends Egg { //继承自Egg类,并且也有内部类Yolk
19 # private static Test monitor = new Test();
20 # public class Yolk {
21 # public Yolk() { System.out.println("BigEgg.Yolk()"); }
22 # }
23 # public static void main(String[] args) {
24 # new BigEgg(); //实例化BigEgg对象,将首先到其基类中查找无参构造函数
25 # monitor.expect(new String[] { //将首先输出New Egg(); 然后再实例化其内部类赋值给y;并将输出Egg.Yolk()
26 # "New Egg()", //而并没有实例化BigEgg中的Yolk对象
27 # "Egg.Yolk()"
28 # });
29 # }
30 # } ///:~