标签:
publicclassTest{
publicstaticvoid main(String args []){
User u1 =newUser();
User u2 =newUser();
User u3 = u1;
boolean b1 = u1 == u2;
boolean b2 = u1 == u3;
System.out.println(b1);
System.out.println(b2);
}
}
classUser{
String name;
int age;
public boolean equals(Object obj){
if(this== obj){
returntrue;
}
//instanceof作用:判断前面的对象是不是后面对象的类型
boolean b = obj instanceof User;
if(b){
//obj向下转型为User对象
User u =(User)obj;
if(this.age == u.age &&this.name.equals(u.name)){
returntrue;
}
else{
returnfalse;
}
}
else{
returnfalse;
}
}
}
publicclassTest{
publicstaticvoid main(String args []){
User u1 =newUser();
User u2 =newUser();
User u3 =newUser();
u1.name ="zhagnsan";
u1.age =12;
u2.name ="lisi";
u2.age =12;
u3.name ="zhagnsan";
u3.age =12;
System.out.println(u1.equals(u2));
System.out.println(u1.equals(u3));
}
}
标签:
原文地址:http://www.cnblogs.com/arroneve/p/5815474.html