public class Person { private String name; private int age;
//----------------Construire------------------ public Person(String name) { this(name,18);//调用下面那个构造方法,age默认为18岁 }
public Person(String name, int age) { this.name = name; this.age = age; }
public Person(int age) { this("???",age);//调用上面那个构造方法,无名氏 }
//-----------------GETTER------------- public String getName() { return this.name; } //-----------------SETTER------------- public void setName(String name) { this.name = name; } }
1 2 3 4 5 6 7 8
public class TestPerson { public static void main(String[] args) { Person xiaoHong = new Person("xiao hong"); //调用上面第一个构造方法 Person xiaoMing = new Person("xiao ming" ,18);//调用上面第二个构造方法