2015-10-13 121 views
2

我想从圆柱访问圆的半径。当我从Cylinder中调用基类getRadius()方法时,什么都没有发生。有人能指出我做错了什么吗?我会很感激。从基类中访问一个不返回值的子类的获取方法

Circle类:

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); 
} 
} 

Cylinder类:

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()函数内(忽略点代码):

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) 
+0

您并未设置圆柱体的半径......换句话说:圆柱体不仅仅是传递给构造函数的“高度”。 – Tom

回答

0

你是不是对你的Cylinder设定的radius值,“默认”值是0.0:

Circle(){ 
    this.radius = 0.0; 
} 

您可以设置它像这样

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

或修改缸构造:

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(通过这个对象的构造函数)。

1

您的Cylinder中的radius为0.0,因为您从未为其分配值。 这将有助于:

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

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

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

你需要调用setRadius你的汽缸对象上,或者添加2参数的构造函数(高度,半径)到Cyclinder

1

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

你想,而不是被调用超级构造函数定义半径什么:

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

而是单独高度的构造。用0初始化hight的默认构造函数可以,因为那里的半径也不重要。