2016-03-06 50 views
0

我有一個抽象類GeometricObject,它由基類Rectangle擴展。在父,我提供了兩個構造函數:Java - 構造函數未定義

public abstract class GeometricObject { 
    private String color = "white"; 
    private boolean filled; 
    private java.util.Date dateCreated; 

    /** Construct a default geometric object */ 
    protected GeometricObject() { 
    dateCreated = new java.util.Date(); 
    } 

    /** Construct a geometric object with color and filled value */ 
    protected GeometricObject(String color, boolean filled) { 
    dateCreated = new java.util.Date(); 
    this.color = color; 
    this.filled = filled; 
    } 

在矩形類,我創建了另一個構造函數,這是應該調用GeometricObject參數的構造函數。糾正我,如果我錯了,但這是給「矩形」對象的「顏色」和「填充」值的正確方法。

public class Rectangle extends GeometricObject implements Comparable<Rectangle>{ 
    private double width; 
    private double height; 

    public Rectangle() { 
    } 

    **public Rectangle(double width, double height, String color, boolean filled) { 
    super(color, filled); 
    this.width = width; 
    this.height = height; 
    }** 

然而,Eclipse的給我下面的錯誤超級旁(顏色,填充)聲明: 「構造GeometricObject(字符串,布爾)未定義」

我缺少什麼?

+1

似乎在這裏很好 - http://ideone.com/msFeyz。你能構建一個[最小測試用例](http://stackoverflow.com/help/mcve)嗎? –

+0

不可思議。在您的評論之後,我添加了一個新的測試類並使用構造函數創建了一個實例,並且突然間錯誤消失了。神祕的IDE ......謝謝! – DR29

回答

0

構造函數調用看起來不錯,我已經在Eclipse中試過了,它編譯時沒有任何錯誤。然而,由於矩形類實現可比,我們需要重寫compareTo方法如下圖所示:

@Override 
public int compareTo(Rectangle o) { 
    // TODO Auto-generated method stub 
    return 0; //should be replaced by comparison logic 
} 
+0

爲了簡單起見,我刪除了類似的方法,謝謝提及它!在您的評論之後,我添加了一個新的測試類並使用構造函數創建了一個實例,並且突然間錯誤消失了。神祕的IDE ......謝謝! – DR29