//: PolyConstructors.java
// Constructors and polymorphism
// don't produce what you might expect.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);
}
} ///:~在Glyph中,draw()方法是“抽象的”(abstract),所以它可以被其他方法覆盖。事实上,我们在RoundGlyph中不得不对其进行覆盖。但Glyph构建器会调用这个方法,而且调用会在RoundGlyph.draw()中止,这看起来似乎是有意的。但请看看输出结果:Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
以上是书上的原话,我对java不是很了解。输出结果前2行我勉强可以理解。
那位高高手,可以帮忙,帮我解释这个程序是怎么一步步run的,最后出现这4行输出结果的。
小弟感谢ing
abstract void draw();
Glyph() {
super();
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}class RoundGlyph extends Glyph {
int radius = 1;
RoundGlyph(int r) {
super();//默认执行构造器,事实上是执行下面的代码:
//System.out.println("Glyph() before draw()");
//draw();//这个调用的是RoundGlyph 自己类中的draw方法 System.out.println("RoundGlyph.draw(),
//System.out.println("Glyph() after draw()");
radius = r;
System.out.println(
"RoundGlyph.RoundGlyph(), radius = "
+ radius);//这个是最后一句了
}
void draw() {
System.out.println(
"RoundGlyph.draw(), radius = " + radius);
}
}
这个我理解,但是接下来我不懂了。
接下来的步骤,请详细告诉我,谢谢哥哥姐姐