我开始学习Java,我无法理解"Thinking in Java"一书中的一个例子.在这个例子中,作者表示,他说"简单使用'this'关键字":
//Leaf.java
//simple use of the "this" keyword
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
}
Run Code Online (Sandbox Code Playgroud)
当上面的代码确实正常工作时,我无法理解increment()返回的方法.
它不是变量i,它不是对象x?我只是不明白.我试图修改程序,了解它(如更换return this用return i或print x代替i),但是编译器显示我的错误.