java中this这个概念初学者非常难理解,请举例说明

来源:互联网 发布:三星软件下载中心 编辑:程序博客网 时间:2024/06/05 05:03
继上一小节,(3.一个对象可能有多个参考)this是n多个参考当中的一个参考,是系统赋予的,与生俱来的,当对象产生了以后就自动具有了this这个参考了!this这个对象指向他自己。Any object has a “this” reference which refer to itself. So, You can use this reference to refer to the current object.


class MyTestDate {
    int year;
    int month;

    MyTestDate(int year, int month, int day) {

        this.year = year;//this.year指前面类范围的变量 int year。
        this.month = month;
    }

    void setYear(int year) {
        this.year = year;

    }

    void setMonth(int month) {
        this.month = month;

    }



    public String toString() {
        return "" + year + "/" + month ;
    }
}

public class Test {
    public static void main(String[] args) {
        MyTestDate date = new MyTestDate(2009, 7, 18);
。。。。。。。。。。。。。。。。
详情请见:
http://www.mark-to-win.com/JavaBeginner/JavaBeginner2_web.html#This
0 0