this 的本质是“创建好的对象的地址”,指的是当前建好的对象:
注意:不能用于static方法
第一种用法,区分二义性:
public class Test {
private int x;
private int y;
Test(int x,int y){
this.x = x;//变量名相同,区分形参和实参
this.y = y;
}
}
第二种方法,使用this调用构造器,必须位于第一句
public class Test {
private int x;
private int y;
private int z;
Test(int x,int y){
this.x = x;//变量名相同,区分形参和实参
this.y = y;
}
Test(int x,int y,int z){
this(x,y);
this.z = z;
}
}