看书看到这有点疑问:
通过这两段代码发现无参构造函数和有参构造函数之间是能够通过this相互调用的。
public class N1 {
N1(){
this(""); //在无参构造函数中调用有参构造函数
System.out.println("无参");
}
N1(String a){
System.out.println("有参");
}
public static void main(String[] args) {
new N1();
}
}
运行结果:有参 无参
public class N1 {
N1(){
System.out.println("无参");
}
N1(String a){
this(); //在有参构造函数中调用无参构造函数
System.out.println("有参");
}
public static void main(String[] args) {
new N1("");
}
}
运行结果:无参 有参
注:构造函数调用必须是构造函数中的第一个语句