java的this关键字

class point{
int x;
int y;
point(int x,int y){
this.x=x;//如果形参和属性名相同,为了区分开来,必须要在属性名前加this
y=y;//若不加this,则其值还是默认定义的0
}
}

public class This {
public static void main(String[] args) {
point p = new point(1,2);
System.out.println("x的值为:"+p.x);
System.out.println("y的值为:"+p.y);
}
}

输出结果:

结果很明显:

  如果形参和成员变量名相同,则this关键字是为了区分对象的成员变量和构造方法里的形参

原文地址:https://www.cnblogs.com/sunbr/p/11623980.html