Constructor Constructor

package Test;
/*
* If no constructor is given, the system will give a null-argument constructor by default; if a constructor is given, the system will not provide a no-argument constructor
*
*/
public class Constructor {

  public static void main(String[] args) {
    PersonStu p=new PersonStu();
    p.show();
    PersonStu p1=new PersonStu("kobe",39);
    p1.show();
  }
}
class PersonStu {
  private String name;
  private int age;
  //public Person() {} 默认空参构造
  public PersonStu() {
    System.out.println("空参构造");
  }
  public PersonStu(String name,int age) {
    this.name=name;
    this.age=age;
    System.out.println("有参构造");
  }
  public void show() {
    System.out.println(name+","+age);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324998250&siteId=291194637