2013-07-15 7 views
0

我知道其他人有一個非常類似的問題,我和我曾試圖應用這些答案,我的代碼,但它仍然無法正常工作,所以我希望你的球員之一將能夠看看我的代碼,並解釋在哪裏出了問題......直角平面,找不到符號錯誤

這是我的代碼有:二

public class Square extends Rectangle{ 
String Colour; 

    public Square (int x, int y, int h, int w, String Co){ 
    super (x,y,h,w); 
    Colour=Co; 
    System.out.println("Constructing a Square now"); 
    } 
     public void showColour(){ 
     System.out.println("The colour of the square is " + Colour); 
     } 
} 

部分:

public class InheritProgram { 
public static void main (String [] args){ 
Square One= new Square (10,20, 15, 15, "blue"); 

Square colour =new Square(); 
colour.showColour(); 

//GeometricShape center= new displayCenter(); 

} 
} 

這是錯誤Im g ETTING:

C:\Users\Karen\Documents\Java\Lab8-1\InheritProgram.java:5: error: constructor Square in class Square cannot be applied to given types; 
Square colour =new Square(); 
      ^
    required: int,int,int,int,String 
    found: no arguments 
    reason: actual and formal argument lists differ in length 
1 error 

Tool completed with exit code 1 

任何幫助將是非常讚賞

+0

爲什麼你方構造帶的高度和寬度,高度平方=總是它的寬度。問題就出現了,因爲你必須提供價值,當你創建一個新的正方形,因爲你在這裏做的:第一廣場=新廣場(10,20,15,15日,「藍」);,您還必須在這裏做的:方形顏色=新廣場(); – w4etwetewtwet

回答

5

在這一行:

Square colour =new Square(); 

...你試圖調用無參數的構造函數Square - 但你還沒有申報。你只聲明瞭5個參數的構造,所以這是你必須以創建一個新的實例來使用的。

不知道爲什麼你要創建第二個實例 - 爲什麼不直接致電OneshowColour?如果你的縮進與問題中的內容相匹配,那麼也修改它 - 它會讓你的代碼很多更容易閱讀。大多數IDE讓你很容易格式化代碼。)

+0

喬恩斯基特太感謝你了,我在它的眼前了這麼久,甚至沒有看到什麼太明顯的錯誤我甚至沒有注意到它...絕對愛你現在:d –

0

除了上面的答案,你應該已經注意到堆棧跟蹤,這說明了一切。

Square colour =new Square(); 
required: int,int,int,int,String 
found: no arguments 

它說,它需要使用參數INT,INT,INT,INT,字符串(唯一的)構造函數,但你卻叫一個無參數的構造函數。