gpt4 book ai didi

Java父类(super class)继承

转载 作者:行者123 更新时间:2023-11-30 07:15:42 26 4
gpt4 key购买 nike

我有一个扩展为两个子类(CircleFromSimpleGeometricObject 和 RectangleFromSimpleGeometricObject)的父类(super class) (SimpleGeometricObject),以及一个调用 CircleFromSimpleGeometricObject 和 RectangleFromSimpleGeometricObject 的类,称为 TestCircleRectangle。在调试器之后,对于子类 CircleFromSupleGeometricObject,这行代码:

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

以某种方式调用父类(super class) 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());
}

}

最佳答案

public CircleFromSimpleGeometricObject(double radius) 这样的构造函数必须始终在第一行包含对其父类(super class)构造函数的调用;如果你没有明确地这样做,编译器将无形地插入一个对父类(super class)的无参数构造函数的调用,如果它有的话。这就是这里发生的事情;构造函数自动调用 public SimpleGeometricObject()

构造函数可以像这样调用父类(super class)构造函数:

super();

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

附言正如评论者所提到的,您的类(class)名称确实很奇怪且不必要; CircleRectangle 就足够了。

关于Java父类(super class)继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17538377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com