class Test{
public static void main(String [] args)
{
User u1= new User();
User u2= new User();
User u3= u1;
//==比较两端是否指向同一个对象
boolean b =u1==u2;
boolean b1 = u1== u3;
System.out.println("b="+b);
System.out.println("b1="+b1);
b =u1.equals(u2);
b1 = u1.equals(u3);
System.out.println("b="+b);
System.out.println("b1="+b1);
}
}
复写equals
class User{
private String name;
private int age;
public boolean equals(object obj)
{
if(this == obj)
{
boolean b = obj instanceof User;
if(false == b)
{
return false;
}
User u = (User)obj;
if(this.age == u.age && this.name.equals(u.name))
{
}else
{
return false;
}
}else
{
return false;
}
return true;
}
}