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; }}
转载于:https://www.cnblogs.com/hezijava/p/5513738.html
相关资源:彻底理解Java中this 关键字