[英]Accessing a get method in base class from a child class not returning value
我正在嘗試從圓柱體訪問“圓”中的半徑。 當我從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)
Cylinder
的radius
為0.0,因為您從未為其分配值。 這將有助於:
Cylinder cylinder = new Cylinder(height);
cylinder.setRadius(some value);
最好添加一個允許設置半徑和高度的構造函數:
public Cylinder(double radius, double height){
super(radius);
this.height = height;
}
您需要在Cylinder對象上調用setRadius,或向Cyclinder添加2個參數的構造函數(高度,半徑)
在您的Cylinder
的構造函數中,您沒有顯式調用Circle
任何超級構造函數。 因此,默認構造函數被隱式調用,它將半徑設置為0。
您想要的是調用定義半徑的超級構造函數:
public Cylinder(double height, double radius){
super(radius);
this.height = height;
}
而不是僅具有高度的構造函數。 可以用0初始化hight的默認構造函數是可以的,因為那里的半徑也不重要。
您沒有為Cylinder
設置radius
的值,並且'default'值為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);
雖然,我會更好,如果您可以進行依賴注入 ,並使用Circle
創建一個Cylinder
的實例(將該對象傳遞給構造函數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.