java一个内部类覆盖的问题,这段代码的运行顺序是什么?
//:bigegg2.java//properinheritanceofaninnerclassclassegg2{protectedclassyolk{publicyo...
//: bigegg2.java
// proper inheritance of an inner class
class egg2 {
protected class yolk {
public yolk() {
system.out.println("egg2.yolk()");
}
public void f() {
system.out.println("egg2.yolk.f()");
}
}
private yolk y = new yolk();
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();
}
} /*egg2.yolk()
new egg2()
egg2.yolk()
bigegg2.yolk()
bigegg2.yolk.f()
///:~
为什么输出的第一个是egg2.yolk(),而不是new egg2()。不是应该先运行egg2的构造器吗?
这对个程序在《java编程思想》第四版的213页。 展开
// proper inheritance of an inner class
class egg2 {
protected class yolk {
public yolk() {
system.out.println("egg2.yolk()");
}
public void f() {
system.out.println("egg2.yolk.f()");
}
}
private yolk y = new yolk();
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();
}
} /*egg2.yolk()
new egg2()
egg2.yolk()
bigegg2.yolk()
bigegg2.yolk.f()
///:~
为什么输出的第一个是egg2.yolk(),而不是new egg2()。不是应该先运行egg2的构造器吗?
这对个程序在《java编程思想》第四版的213页。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询