问题
如果在一个构造器内部,调用正在构造的对象的某个动态绑定方法,那会发生什么情况呢?
代码
class Glyph {
void draw() {
print("Glyph.draw()");
}
/**
* 在基类的构造器中,调用动态绑定的方法(此方法被导出类覆盖)
*
*/
Glyph() {
print("Glyph() before draw()");
draw();
print("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
print("RoundGlyph.RoundGlyph(), radius = " + radius);
}
@Override
void draw() {
print("RoundGlyph.draw(), radius = " + radius);
}
}
class RectangularGlyph extends Glyph {
private int width = 4;
private int height = 5;
RectangularGlyph(int width, int height) {
this.width = width;
this.height = height;
print("RectangularGlyph.RectangularGlyph(), width = " + width + ", height = " + height);
}
void draw() {
print("RectangularGlyph.draw(), area = " + width *
height);
}
}
/**
* 在基类构造器中,调用动态绑定的方法,则导出类方法被调用时,导出类的成员实际上还未被初始化;
* 当调用到导出类的构造器时,导出类的成员已经被初始化了;
*/
public class E15_PolyConstructors2 {
public static void main(String[] args) {
new RoundGlyph(5);
print("------------------------------");
new RectangularGlyph(2, 2);
}
}
输出结果
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
------------------------------
Glyph() before draw()
RectangularGlyph.draw(), area = 0
Glyph() after draw()
RectangularGlyph.RectangularGlyph(), width = 2, height = 2
结论
类初始化的实际过程是:
- 在其他任何事物发生之前,将分配给对象的存储空间初始化成二进制的零。
这也解释了为何基类构造器中调用动态绑定方法时为何radius = 0
、area = 0
- 调用基类构造器
- 按照声明的顺序调用成员的初始化方法
- 调用导出类的构造器主体
这解释了导出类构造器中为何:radius = 5
、width = 2, height = 2
网友评论