简体   繁体   中英

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

What could be wrong with my code? The errors am being warned of by the compiler are not helping me understand the problem.

There are 5 problems with your code. Firstly, variables should begin with lower case letters, so I have changed Age to age . Secondly, your constructor should not be in the main method. Thirdly, you want printf rather than println . Also you need to get rid of those + signs. + is an operator that requires 2 String s (one on either side), not just one String . Finally, I assume you mean \\t rather than /t . I have changed this and also added two line breaks.

The corrected code is

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

Constructor are a special kind of method. These generally need to be outside of other methods.

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

Constructors are in every class by default. if you didnt have a constructuro in your Person class then you would have a default one so you could call

new Person();

By adding a constructor with string, string, int then you loose this default constructor. If you want a default one with no input parameters then you will need to include it again

Person(){}

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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