this 关键字
在类的方法定义中使用this关键字代表使用该方法的对象的引用
在必须指出当前使用方法的对象是谁时要使用this
有时使用this可以处理方法中的成员变量和参数重名的情况
this可以看作是一个变量,它的值是当前对象的引用
public class Leaf {
int i = 0;
Leaf(int i) {
this.i = i;
}
Leaf increament() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf leaf = new Leaf(100);
leaf.increament().increament().print();
}
}
输出: i = 102

2371

被折叠的 条评论
为什么被折叠?



