初始化的实际过程:
1.在其他任何事物发生之前,将分配给对象的存储空间初始化成二进制的0。
2.调用基类构造器。(这个步骤会不断反复递归下去,首先是构造这种层次结构的根,然后是下一层子类,等等,直到最低层的子类)
3.按声明顺序调用成员的初始化方法。
4.调用子类的构造器主体。
构造器内部的多态方法的行为
class Glyph {
void draw() { print("Glyph.draw()"); }
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() {
System.out.println("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
}
/* 输出结果:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
*/