2015-10-23 61 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 
    } 
}