class Person{
String name;
int age;
public Person(String n , int a){
name=n; age=a;
}
}
public class Person{
public Person(String name, int age, Date d) {this(name,age);...}
public Person(String name, int age) {...}
public Person(String name, Date d) {...}
public Person(){...}
}
举例1:
package day06;
public class Person6 {
/**
* 构造器的重载,就是为了方便 调用方 可以灵活的创建出不同需要的对象
* 重载的多个构造方法实际上就是相当于提供了多个初始化new对象的模版
*/
public Person6() { //构造器的无参重载
}
public Person6(int a) { //构造器的重载(有一个int参数)
}
public Person6(Sring n) { //构造器的重载(有一个String参数)
}
public Person6(int a, String n) { //构造器的重载(...)
age = a;
name = n;
}
public int age;
public String name;
}
举例2:
public class Person {
private String name;
private int age;
private Date birthDate;
public Person(String name, int age, Date d) {
this.name = name;
this.age = age;
this.birthDate = d;
}
public Person(String name, int age) {
this(name, age, null);
//this.name=name; this.age=age; this.birthDate=null;
}
public Person(String name, Date d) {
this(name, 30, d);
//this.name=name; this.age=30; this.birthDate=d;
}
public Person(String name) {
this(name, 30); //this.name=name; this.age=30;
}
}