java 类与对象 2---this关键字

来源:互联网 发布:office mac 2010破解 编辑:程序博客网 时间:2024/05/23 01:26

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

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

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

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

public class Dog{    int i = 0;    Dog(int i){        this.i = i;    }    Dog sceam(){        i++;        return this;    }    void print(){        System.out.println("i = "+i);    }    public static void main(String[] args){        Dog dog = new Dog(49);        Dog.sceam().sceam().print();    }}