使用"返回此"时返回课程的内容是什么?

ble*_*one 6 java

我开始学习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 thisreturn iprint x代替i),但是编译器显示我的错误.

Jun*_*san 2

return this;
Run Code Online (Sandbox Code Playgroud)

将返回当前对象,即您用来调用该方法的对象。在您的情况下,将返回x类型的对象。Leaf