Javakeyword之this

Posted gavanwanggw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javakeyword之this相关的知识,希望对你有一定的参考价值。

this的作用:
1) this是当前对象的一个引用。便于对当前对象參数的使用。
2)能够返回对象的自己这个类的引用。同一时候还能够在一个构造函数其中调用还有一个构造函数

this演示样例:
public class ThisDemo {

    public ThisDemo()
    {
        this.number=20;
    }
    public ThisDemo(int number,int number2)
    {
        this();//调用无參数的构造函数
        this.number2=number2;
    }
    ThisDemo increment(){
        number++;
        number2++;
        return this;
    }
    private void print(){
        System.out.println("number="+number+";number="+number2);
    }
    public static void main(String[] args) {
        ThisDemo tt=new ThisDemo();
        tt.increment().increment().increment().print();
    }
}

以上是关于Javakeyword之this的主要内容,如果未能解决你的问题,请参考以下文章