如何在Java中使用构造函数

时间:2015-04-22 22:52:46

标签: java constructor

public class Person{

    public static void main(String[] args){
        //Class constructor
        Person(String name, String status, int Age){
            this.name = name;
            this.status = status;
            this.Age = Age;
        }
        //Object creation
        Person one = new Person("John", "Single", 18);
        Person two = new Person("Kez", "Single", 21);
        Person three = new Person("Bob", "Married", 31);

        //Print out attributes
        System.out.println("Person one Profile: %s/t%s/t%d", +one.name,    +one.status, +one.Age);
        System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
        System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
    }
}

我的代码有什么问题? 编译器警告的错误并没有帮助我理解这个问题。

3 个答案:

答案 0 :(得分:1)

您的代码有5个问题。首先,变量应以小写字母开头,因此我将Age更改为age。其次,您的构造函数不应该在main方法中。第三,您需要printf而不是println。你还需要摆脱那些+的迹象。 +是一个运算符,需要2 String s(每边一个),而不只是一个String。最后,我假设您的意思是\t而不是/t。我已经更改了这个并添加了两个换行符。

更正的代码是

public class Person {

    private String name;
    private String status;
    private int age;

    Person(String name, String status, int age){
        this.name = name;
        this.status = status;
        this.age = age;
    }

    public static void main(String[] args) {
        //Object creation
        Person one = new Person("John", "Single", 18);
        Person two = new Person("Kez", "Single", 21);
        Person three = new Person("Bob", "Married", 31);

        //Print out attributes
        System.out.printf("Person one Profile: %s\t%s\t%d\n", one.name, one.status, one.age);
        System.out.printf("Person two Profile: %s\t%s\t%d\n", two.name, two.status, two.age);
        System.out.printf("Person three Profile: %s\t%s\t%d", three.name, three.status, three.age);
    }
}

答案 1 :(得分:0)

构造函数是一种特殊的方法。这些通常需要超出其他方法。

//Class constructor
Person(String name, String status, int Age){
    this.name = name;
    this.status = status;
    this.Age = Age;
}
public static void main(String[] args){

  //Object creation
  Person one = new Person("John", "Single", 18);
  Person two = new Person("Kez", "Single", 21);
  Person three = new Person("Bob", "Married", 31);

  //Print out attributes
  System.out.println("Person one Profile: %s/t%s/t%d", +one.name,    +one.status, +one.Age);
  System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
  System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
}

默认情况下,构造函数位于每个类中。如果你在Person类中没有一个constructuro,那么你将有一个默认的,所以你可以调用

new Person();

通过添加带有string,string,int的构造函数,然后松开此默认构造函数。如果你想要一个没有输入参数的默认值,那么你需要再次包含它

Person(){}

答案 2 :(得分:0)

试试这个:

class Test{
  public static void main(String[] args){

    //Object creation
    Person one = new Person("John", "Single", 18);
    Person two = new Person("Kez", "Single", 21);
    Person three = new Person("Bob", "Married", 31);

    //Print out attributes
    System.out.printf("Person one Profile:\t %s\t%s\t%d%n", one.name, one.status, one.age);
    System.out.printf("Person two Profile:\t %s\t%s\t%d%n", two.name, two.status, two.age);
    System.out.printf("Person three Profile: %s\t%s\t%d%n", three.name, three.status, three.age);
  }
}

//Class constructor
class Person{
  //object fields
  String name;
  String status;
  int age;

  //constructor
  Person(String name, String status, int age){
    //overloading... if you use different names you can remove 'this'
    this.name = name;
    this.status = status;
    this.age = age;
  }
}