this关键字在构造函数中的应用

/**
* Created by 刘朋程.on 2014-6-12.博客园

this的用法

1、用于区分同名变量的情况(成员变量和局部变量同名的时候)

2、用于构造函数之间互相调用。this语句只能定义在构造函数的第一行,以为初始化要先执行。
*/
class this_persion {
    private String name;
    private int age;

    this_persion()
    {

}
    this_persion(String name)
    {
        this.name = name ;
    }
    this_persion(String name,int age)
    {
        this(name);
        this.age = age ;
    }
    public boolean compare(this_persion p)
    {
        return this.age == p.age;
    }

}

Created by 刘朋程.on 2014-6-12.博客园
class this_persionDomo
{
   public static void main(String[] args)
   {
       this_persion p1 = new this_persion("张三",35);
       this_persion p2 = new this_persion("张三",35);
       boolean b = p1.compare(p2);
       System.out.println(b);
   }
}

Created by 刘朋程.on 2014-6-12.博客园

原文地址:https://www.cnblogs.com/liupengcheng/p/3783708.html