主题:[讨论]已解决
中文版P213,英文版P384
class Egg2{
public Egg2(){System.out.println("New Egg2()");}
protected class Yolk{
public Yolk(){ System.out.println("Egg2.Yolk()");}
}
private Yolk y=new Yolk();
public void insertYolk(Yolk yy){y=yy;}
}
public class BigEgg2 extends Egg2{
public class Yolk extends Egg2.Yolk{
public Yolk(){System.out.println("BigEgg2.Yolk()");}
}
public BigEgg2() {insertYolk(new Yolk());}
public static void main(String[] args){
Egg2 e2=new BigEgg2();
}
}
我做了部分修改去掉了f()的调用。
实际输出:
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
我想问的问题是,怎么会先调用了内部类的构造函数呢?即先输出了,Egg2.Yolk()?
哪位朋友能解释下吗?
class Egg2{
public Egg2(){System.out.println("New Egg2()");}
protected class Yolk{
public Yolk(){ System.out.println("Egg2.Yolk()");}
}
private Yolk y=new Yolk();
public void insertYolk(Yolk yy){y=yy;}
}
public class BigEgg2 extends Egg2{
public class Yolk extends Egg2.Yolk{
public Yolk(){System.out.println("BigEgg2.Yolk()");}
}
public BigEgg2() {insertYolk(new Yolk());}
public static void main(String[] args){
Egg2 e2=new BigEgg2();
}
}
我做了部分修改去掉了f()的调用。
实际输出:
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
我想问的问题是,怎么会先调用了内部类的构造函数呢?即先输出了,Egg2.Yolk()?
哪位朋友能解释下吗?