从子类访问基类中的get方法而不返回值

时间:2015-10-13 19:12:08

标签: java

我正在尝试从Cylinder访问Circle中的半径。当我从Cylinder中调用基类getRadius()方法时,什么也没发生。有人可以指出我做错了什么吗?我很感激。

圈子类:

public class Circle extends Point {
double radius;

Circle(){
    this.radius = 0.0;
}    
Circle(double radius){
    this.radius = radius;
}    
public double getRadius(){
    return radius;
}    
public void setRadius(double radius){
    this.radius = radius;
}    
public double area(){
    double area = Math.PI * Math.pow(radius, 2);
    return area;
}    
@Override
public String toString(){
    return String.format("Radius: " + radius);
}
}

圆柱类:

public class Cylinder extends Circle{
private double height;

public Cylinder(){
    this.height = 0.0;
}
public Cylinder(double height){
    this.height = height;
}
public double getHeight(){
    return height;
}    
public void setHeight(double height){
    this.height = height;
}    
public double volume(double height){
    double volRad = super.getRadius();
    double volume = Math.PI * Math.pow(volRad, 2) * height;
    return volume;
}    
@Override
public double area(){
    double areaRad = super.getRadius();
    double area = Math.PI * Math.pow(areaRad, 2);
    return area;
}    
@Override
public String toString(){
    return String.format("Height: " + height);
}
}

main()函数内的代码(忽略Point代码):

    double radius = 3.2;
    double height = 5.1;
    Point point = new Point(3, 4);
    Circle circle = new Circle(radius);
    Cylinder cylinder = new Cylinder(height);

    //Print out objects via overridden toString() method
    System.out.println("Point properties: " + point.toString());
    System.out.println("Circle properties: " + circle.toString());
    System.out.println("Cylinder properties: " + cylinder.toString());

    //Invoke area() in circle object
    DecimalFormat df = new DecimalFormat("#.##");

    System.out.println("\nCircle area: " + df.format(circle.area()));

    //Invoke area() and volume() in cylinder
    System.out.println("\nCylinder area: " + df.format(cylinder.area()));
    System.out.println("Cylinder volume: " + df.format(cylinder.volume(height)));

这是我的输出:

Point properties: X-value: 3 Y-value: 4
Circle properties: Radius: 3.2
Cylinder properties: Height: 5.1

Circle area: 32.17

Cylinder area: 0
Cylinder volume: 0
BUILD SUCCESSFUL (total time: 0 seconds)

4 个答案:

答案 0 :(得分:1)

radius中的Cylinder为0.0,因为您从未为其指定值。 这会有所帮助:

Cylinder cylinder = new Cylinder(height);
cylinder.setRadius(some value);

最好添加一个允许设置半径和高度的构造函数:

public Cylinder(double radius, double height){
    super(radius);
    this.height = height;
}

答案 1 :(得分:1)

您需要在Cylinder对象上调用setRadius,或者向Cyclinder添加2参数构造函数(高度,半径)

答案 2 :(得分:1)

Cylinder的构造函数中,您没有显式调用Circle的任何超级构造函数。因此,隐式调用默认构造函数,将半径设置为0.

你想要的是调用定义半径的超级构造函数:

public Cylinder(double height, double radius){
    super(radius);
    this.height = height;
}

而不是仅具有高度的构造函数。使用0初始化hight的默认构造函数是可以的,因为半径也无关紧要。

答案 3 :(得分:0)

您没有为radius设置Cylinder的值,并且'默认'值为0.0:

Circle(){
    this.radius = 0.0;
} 

您可以像这样设置

Cylinder cylinder = new Cylinder(height);
cylinder.setRadius(some value);

或者修改Cylinder构造函数:

public Cylinder(double height, double radius){
    super(radius); //call the correct constructor, not the one that set 0.0 as default.
    this.height = height;
}

并创建如下实例:

Cylinder cylinder = new Cylinder(height, radius);

尽管如此,如果你能拥有dependency injection,我会更好,并使用Cylinder创建Circle的实例(将此对象传递给构造函数)。