package myTest;
abstract class Glyph {
abstract void draw();
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
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);
}
}
这个是thinking in java书上的一个例子,解释并没有看的很懂,这里实例化了一个Roundlyph对象,会先调用父类构造器,当调用父类构造器的时候执行了覆盖的draw方法,但是此时父类没有radius,所以没有初始化这个值,但是子类成员的初始化还没有开始,所以这个时候radius应该是都没生命的啊,为什么还会输出0.
只有11c币,全身家当了