目前正在对抽象类和接口(interface)进行实践测试,遇到了这个问题:
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** Default constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
为什么输出是BEDC?我认为 new Circle9() 会首先调用 Circle 9 类的无参数构造函数,打印 A,然后打印其他字母,但我很难理解代码的路径。
最佳答案
跟踪调用堆栈:
- new Circle9() 调用 Circle9()
- Circle9() 调用 this(1.0)
- Circle9(double) 调用 this(radius, "white", false)
- Circle9(double, String, boolean) 调用 super(color,filled)
- GeometricObject(String, boolean) 打印 B
- Circle9(double, String, boolean) 打印 E
- Circle9(double) 打印 D
- Circle9() 打印 C
关于java - 抽象类: output of this code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18776123/