java - 抽象类: output of this code

标签 java eclipse

目前正在对抽象类和接口(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/

相关文章:

java - 加载图像

java - 如何使用 Eclipse 启动 Web 应用程序

c++ - Eclipse Cdt Indexer - 如何定位语法错误和未解析的名称

java - 通过 servlet 访问 Dropwizard JVM 指标时出现问题

java - "Could not find main method from given launch configuration"使用Java+Scala+Slick2D时

Java Eclipse Maven 版本控制

java - Web 服务将参数传递到另一个类/项目

java - 在 Eclipse 中添加外部 jar

JavaFX:是否可以为整个 TreeView 设置背景颜色?

java - 如何使该策略对象模式类型安全