2015-10-23 60 views
0

我想知道如何在Java中定義構造函數。 我開始寫一段代碼,但我得到錯誤。在Java中定義構造函數

package xyz; 

public class ConstructorExample { 

    public static void main(String[] args) { 

     public ConstructorExample(int x,int y){ 
//   private double x,y; 
     } 

    } 

} 

我可以用main寫類的構造函數還是必須有另一個類?謝謝。

回答

1

你constuctor必須像聲明任何其他方法(但它被稱爲相同,類名,並且不提供任何返程):

public class ConstructorExample { 
    //this is your class fieald 
    private double x,y; 

    //here is the constructor 
    public ConstructorExample(int x,int y){ 
     //set the class field's values, via this (means class), 
     //because the arg names is the same as fields names 
     this.x = x; 
     this.y = y; 
    } 

    public static void main(String[] args) { 
     //here is how you can create a class instance inside the main method 
     ConstructorExample example = new ConstructorExample(1,1); 
    }  
} 

此外,如果你有沒有定義任何構造函數,java會添加默認的,沒有參數。因此,它可能看起來像:

public class ConstructorExample { 
    public static void main(String[] args) { 
     //here is how you can create a class instance inside the main method 
     //with the default constructor 
     ConstructorExample example = new ConstructorExample(); 
    }  
} 

如果你有多個構造函數,那麼你可以再次調用通過this一個從其他,如:

public class ConstructorExample { 
    //this is your class fieald 
    private double x,y; 

    //here is the constructor with the single argument 
    public ConstructorExample(int x){ 
     this.x = x; 
    } 

    //here is the constructor with 2 arguments 
    public ConstructorExample(int x,int y){ 
     //you can call another constructor with the arguments 
     this(x); 
     this.y = y; 
    } 
    public static void main(String[] args) { 
     //here is how you can create a class instance inside the main method 
     ConstructorExample example = new ConstructorExample(1,1); 
    }  

} 
2

您不能在方法內定義構造函數。你必須像在課堂上的其他方法一樣聲明它。

package xyz; 

public class ConstructorExample { 

    public ConstructorExample(int x,int y){ 
//  private double x,y; 
    } 
    public static void main(String[] args) { 

    } 

} 
+1

'ConstructorExample'不繼承默認的構造函數,因爲它有一個參數。 – Stanislav

1

我想知道如何我可以用Java定義構造函數嗎?我開始寫 一段代碼,但我得到錯誤。

錯誤是因爲構造函數是在main()方法內部定義的。需要向外移動。你可以從main方法調用。

Java構造函數是在實例化對象 時調用的特殊方法。換句話說,當你使用新的關鍵字。構造函數 初始化新創建的對象。

package xyz; 

public class ConstructorExample { 

    private int x; 
    private int y; 

    public ConstructorExample(int x,int y){ 
     this.x = x; 
     this.y = y; 
    } 

    public static void main(String[] args) { 
     ConstructorExample example = new ConstructorExample(1,1); 
    } 

    } 
2

殉葬,你可以寫一個構造Main。任何類,枚舉和抽象類都可以有一個構造函數。

使用簡單

public Main(){ 
    //Constructor code here... 
} 

然後調用主只需使用: Main main = new Main();

注:私有,保護和公共改性劑可以使用構造使用。

你在你的代碼

你試圖創建一個方法中的方法得到一個錯誤的原因。

public static void main(String[] args){ 

    public ConstructorExample(int x,int y){ 
     //private double x,y; 
    } 

} 

您試圖在您的main方法中創建構造函數,這是illigal語法。正確的方法是:

class ConstructorExample { 

    //This is the constructor 
    public ConstructorExample(){ 
     System.out.println("Constructor called"); 
    } 

    public static void main(String [] args){ 
     ConstructorExample example = new ConstructorExample();//This calls the constructor when creating the class 
    } 
}