this关键字

this关键字:

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

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

3、使用this处理参数和成员变量重名的问题

4、this看做一个变量,他的值是当前对象的引用

例如:

public class Leaf{

  int i=0;

  Leaf(int i){this.i = i};

  Leaf increament(){

    i++;

    return this;  

  }

  void print(){

    System.out.println("i = ""+i);

  }

  public static void main(String args[]){

      Leaf leaf = new Leaf(100);

      Leaf.increament.increament.print();

    }

}

posted @ 2018-12-24 11:22  雪地里的Alan  阅读(145)  评论(0编辑  收藏  举报