其他分享
首页 > 其他分享> > this关键字

this关键字

作者:互联网

this关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。this的用法和其他对象引用并无不同。   只有当需要明确指出对当前对象的引用时,才需要使用this关键字  
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();
    }
}/* Output:
i = 3
*///:~

  


标签:Leaf,void,关键字,increment,print,public
来源: https://www.cnblogs.com/litingwei/p/16344555.html