class Demothis关键字//哪个对象在调用this所在的函数,this就代表哪个对象。
{           //当定义类中功能时,该函数内部要用到该函数的对象时,这时用this来表示这个对象。
  public static void main(String[] args)
  {
    Person P1=new Person(23);
    Person P2=new Person(24);
    boolean b=P1.compare(P2);
    System.out.println(b);
  }
}
class Person
{
  private int age;
  Person(int age)
  {
    this.age=age;
  }

Person(String name)
  {
    this.name=name;
  }

Person(String name,int age)
  {
    this(name);//this语句只能放语句的第一行: this(name);   this.name=name;    对的      this.name=name; this(name);  错的

    this.age=age;
  }
  public boolean compare(Person P)
  {
    return this.age==P.age;//注意==强制赋值,不然不同类型无法转换: 不兼容的类型: int无法转换为boolean:return this.age=P.age;
  }
}

posted on 2016-05-20 23:06  赫子  阅读(108)  评论(0编辑  收藏  举报