主题:关于private的一个问题,谁能帮我看一下一个,困扰我几天了.
class Egg2 {
protected class Yolk {
public Yolk() {
System.out.println("Egg2.Yolk()");
}
public void f() {
System.out.println("Egg2.Yolk.f()");
}
}
[color=800000]private Yolk y = new Yolk();[/color]
public Egg2() {
System.out.println("New Egg2()");
}
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() {
System.out.println("BigEgg2.Yolk()");
}
public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
}
public BigEgg2() {
insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
}
它的运行结果是
[color=800000]Egg2.Yolk()[/color]
这句运行是不是private Yolk y = new Yolk()起的作用?
BigEgg2()得到实例化后他继承Egg2()的private Yolk y = new Yolk()这个语句吗?
继承的话实例化的话也是Egg2()的Yolk(),为什么?
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
protected class Yolk {
public Yolk() {
System.out.println("Egg2.Yolk()");
}
public void f() {
System.out.println("Egg2.Yolk.f()");
}
}
[color=800000]private Yolk y = new Yolk();[/color]
public Egg2() {
System.out.println("New Egg2()");
}
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() {
System.out.println("BigEgg2.Yolk()");
}
public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
}
public BigEgg2() {
insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
}
它的运行结果是
[color=800000]Egg2.Yolk()[/color]
这句运行是不是private Yolk y = new Yolk()起的作用?
BigEgg2()得到实例化后他继承Egg2()的private Yolk y = new Yolk()这个语句吗?
继承的话实例化的话也是Egg2()的Yolk(),为什么?
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()