下面示范了JAVA中的构造器重载:
public class Person()
{
private String name;
private float height;
private float weight;
public Person(){
}
public Person(String name,float height){
this.name=name;
this.height=height;
}
public Person(String name,float height,float weight){
this(name,height);
this.weight=weight;
}
}
以上代码重载了三个构造器,我们发现在第三个构造器中我们使用this(name,height)调用了第二个构造器,代码变得
更加简洁,减少了冗余。但这里有两个注意点:
1)用this来调用构造器只能再构造器中使用;
2)this调用语句必须作为第一条语句出现。