2

我有一个超类(SimpleGeometricObject),它扩展到两个子类(CircleFromSimpleGeometricObject 和 RectangleFromSimpleGeometricObject),还有一个调用 CircleFromSimpleGeometricObject 和 RectangleFromSimpleGeometricObject 的类,称为 TestCircleRectangle。在调试器之后,对于子类 CircleFromSumpleGeometricObject,这行代码:

public CircleFromSimpleGeometricObject(double radius){
    this.radius = radius;
}

以某种方式调用超类 SimpleGeometricObject:

    /** Construct a default geometric object */
public SimpleGeometricObject() {
    dateCreated = new java.util.Date();
}

我对这是如何发生的以及为什么会发生有些困惑,有人可以帮我理解为什么会发生这种情况吗?以下是所有类的代码。

public class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
public SimpleGeometricObject() {
    dateCreated = new java.util.Date();
}

/** Construct a geometric object with the specified color
 * and filled value */
public SimpleGeometricObject(String color, boolean filled) {
    dateCreated = new java.util.Date();
    this.color = color;
    this.filled = filled;
}

/** Return color */
public String getColor() {
    return color;
}

/** Set a new color */
public void setColor(String color) {
    this.color = color;
}

/** Return filled. Since filled is boolean, 
    its get method is named isFilled */
public boolean isFilled() {
    return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
    this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
    return dateCreated;
}

/** Return a string representation of this object */
public String toString() {
    return "created on " + dateCreated + "\ncolor: " + color +
        " and filled: " + filled;
}
}


public class CircleFromSimpleGeometricObject 
extends SimpleGeometricObject {
private double radius;

public CircleFromSimpleGeometricObject() {

}

public CircleFromSimpleGeometricObject(double radius){
    this.radius = radius;
}

public CircleFromSimpleGeometricObject(double radius,
        String color, boolean filled) {
    this.radius = radius;
    setColor(color);
    setFilled(filled);
}

/** Return radius */
public double getRadius() {
    return radius;
}

/** Set a new radius */
public void setRadius(double radius) {
    this.radius = radius;
}

/** Return area */
public double getArea() {
    return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
    return 2 * radius;
}

/** Return perimeter */
public double getPerimeter() {
    return 2 * radius * Math.PI;
}

/** Print the circle info */
public void printCircle() {
    System.out.println("The circle is created " + getDateCreated() +
            " and the radius is " + radius);
}
}



public class RectangleFromSimpleGeometricObject 
extends SimpleGeometricObject {
private double width;
private double height;

public RectangleFromSimpleGeometricObject() {
}

public RectangleFromSimpleGeometricObject(
        double width, double height) {
    this.width = width;
    this.height = height;
}

public RectangleFromSimpleGeometricObject(
        double width, double height, String color, boolean filled) {
    this.width = width;
    this.height = height; 
    setColor(color);
    setFilled(filled);
}

/** Return width */
public double getWidth() {
    return width;
}

/** Set a new width */
public void setWidth(double width) {
    this.width = width;
}

/** Return height */
public double getHeight() {
    return height;
}

/** Set a new height */
public void setHeight(double height) {
    this.height = height;
}

/** Return area */
public double getArea() {
    return width * height;
}

/** Return perimeter */
public double getPerimeter() {
    return 2 * (width * height);
}

}


public class TestCircleRectangle {
public static void main(String[] args) {
    CircleFromSimpleGeometricObject circle = 
            new CircleFromSimpleGeometricObject(1);
    System.out.println("A circle " + circle.toString());
    System.out.println("The color is " + circle.getColor());
    System.out.println("The radius is " + circle.getRadius());
    System.out.println("The area is " + circle.getArea());
    System.out.println("The diamter is " + circle.getDiameter());

    RectangleFromSimpleGeometricObject rectangle = 
            new RectangleFromSimpleGeometricObject(2, 4);
    System.out.println("\nA rectangle " + rectangle.toString());
    System.out.println("The area is " + rectangle.getArea());
    System.out.println("The perimeter is " + 
        rectangle.getPerimeter());
}

}
4

4 回答 4

4

public CircleFromSimpleGeometricObject(double radius)往常一样的构造函数必须在第一行包含对其超类构造函数的调用;如果你不明确地这样做,编译器将不可见地插入一个对超类的无参数构造函数的调用,如果它有一个。这就是这里发生的事情;构造函数自动调用public SimpleGeometricObject().

构造函数可以像这样调用超类构造函数:

super();

如果需要,您可以包含参数。

PS 正如评论者所提到的,您的班级名称确实很奇怪且不必要;CircleRectangle足够了。

于 2013-07-09T01:10:25.763 回答
0

任何构造函数都会调用超类构造函数(依此类推,直到Object构造函数被调用)。如果您没有显式调用super(),编译器会为您插入它。

看到这一点最明显的方法是在你的超类中没有默认构造函数(没有没有任何参数的构造函数)。在这种情况下,您的子类将不会编译,直到您插入对所需super构造函数的显式调用。

于 2013-07-09T01:11:49.333 回答
0

根据 java 规范,所有对象构造函数都隐式调用其超类的构造函数。想象一下,如果没有:您的dateCreated对象将不会被初始化。

这是我发现的指向 java 规范的博客:

http://www.dribin.org/dave/blog/archives/2004/11/23/java_constructor/

于 2013-07-09T01:15:15.740 回答
0

由于 CircleFormSimpleGeometricObject 扩展了 SimpleGeometricObject,因此在调用构造函数 CircleFormSimpleGeometricObject() 时会自动调用构造函数 SimpleGeometricObject()。

这很有用,因为子类需要来自超类的任何变量都将被初始化,但如果是这种情况,调用 super() 会更安全,它会显式调用超类的构造函数。要指定使用哪个构造函数,可以将特定构造函数调用的变量放入 super() 中。例如,这一行:

    super(String, boolean);

将调用 SimpleGeometricObject 中的相关构造函数。

于 2015-11-09T02:17:13.587 回答