- this关键字代表当前对象
this.属性 操作当前对象的属性
this.方法 调用当前对象的方法
- 封装对象的属性的时候,经常会使用this关键字
比如,用Eclipse给一个类自动生成一个getter/setter方法时public class Person { private String name; public void walk(){ System.out.println("人具有走的能力"); } public String getName() { return name; } public void setName(String name) {//自动生成的setter方法中的参数名与属性名相同 //为了区分属性和参数的概念,所以会在属性前加上 this. 关键字 this.name = name; //this.方法名 ———等于调用当前对象中的某个方法 this.walk(); } }
转载于:https://my.oschina.net/u/3421709/blog/885343