繁体   English   中英

如何在Java中使用构造函数

[英]How to use constructors in Java

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);
    }
}

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

您的代码有5个问题。 首先,变量应以小写字母开头,因此我将Age更改为age 其次,构造函数不应位于main方法中。 第三,您需要printf而不是println 另外,您还需要摆脱那些+号。 +是一个运算符,需要2个String (在任一侧),而不仅仅是一个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);
    }
}

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

//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(){}

尝试这个:

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;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM