JAVA关键字this

来源:互联网 发布:elasticsearch 5.0 sql 编辑:程序博客网 时间:2024/05/30 05:01

1、在类的方法定义中使用的this关键字代表使用该方法的对象的引用。

2、当必须指出当前使用方法的对象是谁时要使用this。

3、有时使用this可以处理方法中成员变量和参数重名的情况。

4、this可以看做是一个变量,它的值是当前对象的引用。

 

public class Leaf{int i=0;Leaf(int i){this.i=i;}Leaf increment(){i++;return this;}void print(){ System.out.println("i= "+i);}public static void main(String[] args){Leaf leaf = new Leaf(100);leaf.increment().increment().print();}}