2017-04-12 77 views
-4

我試圖學習java,而我被卡在'類'部分。他們給了我一個問題,我不知道爲什麼這是錯的。需要幫助解決Square Class問題,特別是代碼?

我已經讀了很多,我不明白爲什麼有大約14個錯誤信息?在所有...

這裏的任務:

// You are to write the constructor specified for this Square class. The Square class 
    // has an instance variable of type double, side, which is the length of each side of 
    // the square. The javadoc has been provided for you to help you tell what you needs to 
    // be done 
// 
// HINT: Write the constructor for the class Square. 
// The constructor will take in a parameter of the type double 
// and assign that parameter to the instance variable side 

這裏是我的代碼:

public class Square(double side) 

    private double side; 

    /** 
    * Constructor for objects of class Square 
    * @param theSide the length of the side of this Square 
    */ 
    public main(double theSide) { 
    side = theSide; 
    } 
    /** 
    * Gets the length of a side of this square 
    * @return the side of this square 
    */ 
    public double getSide() 
    { 
    return side; 
    } 


Compiler error: /tmp/codecheck.XNhW00Z3c8/Square.java:12: error: '{' expected public class Square(double side)^/tmp/codecheck.XNhW00Z3c8/Square.java:12: error: ';' expected public class Square(double side)^/tmp/codecheck.XNhW00Z3c8/Square.java:31: error: reached end of file while parsing }^/tmp/codecheck.XNhW00Z3c8/Square.java:14: error: variable side is already defined in class Square private double side;^
+0

哪裏是錯誤?請發佈錯誤 – abcOfJavaAndCPP

+0

14錯誤,你不能打擾張貼其中之一? – John3136

+0

這裏是錯誤,現在他們下降到4。 –

回答

0

你實際上是在給參數類。在Java類中不應該有一個參數

這裏看看

public class Square(double side) 

正道類

public class Square 
{ 
    private double side; 
    public Square(double theSide) 
    { 
     side=theSide; 
    } 
    public double getSide() 
    { 
     return side; 
    } 


} 

另一個類

public class TestSquare 
{ 

    public static void main(String[] args) 
    { 
     Square square = new Square(25.00); 
     System.out.println("Square sides:"+square.getSide()); 
    } 
} 
+0

是不是參數來保存類中的所有代碼? –

+0

不允許在類頭中有一個參數,類頭中刪除(雙面)誰認爲你呢? – abcOfJavaAndCPP

+0

與類參數類似的任何東西只在類頭中使用泛型。例如。 '公共課程節點'。 @JolandaCarlsen – Ungeheuer