2015-10-13 26 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的默認構造函數可以,因爲那裏的半徑也不重要。

相關問題