- 我们在类的构造器中,可以显式的使用"this(形参列表)"方式,调用本类中指定的其他构造器
- 构造器中不能通过"this(形参列表)"方式调用自己
- 如果一个类中有n个构造器,则最多有 n - 1构造器中使用了"this(形参列表)"
- 规定:"this(形参列表)"必须声明在当前构造器的首行
- 构造器内部,最多只能声明一个"this(形参列表)",用来调用其他的构造器
示例:
publicclassThisTest { privateStringname; privateintage; publicThisTest(){ System.out.println("无参构造器"); }; publicThisTest(Stringname){ this(); System.out.println("有一个参数的构造器"); this.name=name; } publicThisTest(Stringname, intage){ this("name"); System.out.println("有两个参数的构造器"); this.age=age; } publicstaticvoidmain(String[] args) { ThisTestthisTest=newThisTest("Mr.Yu",21); } }
输出结果:
无参构造器有一个参数的构造器有两个参数的构造器Processfinishedwithexitcode0