public class User {
private String id; //对应的用户的SreenName
private String name; //对应的为firstName
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
super();
}
@Override
public boolean equals(Object other){
//System.out.println("调用了新的匹配方法");
if(!(other instanceof User)){
return false;
}else{ //是对象实例
User su=(User)other;
if(su==this){
return true;
}
if(this.id.equals(su.id)&&this.name.equals(su.name)){
//System.out.println("全部相同");
return true;
}else{
//System.out.println("不相同");
return false;
}
}
}
public static void main(String[] args){
User su=new User();
User u1=new User("id1","name1");
User u2=new User("id1","name1");
if(u1.equals(u2)){ //在比较对象时,会调用新的equals方法,否则,将调用默认的equals方法
System.out.println("相同");
}else{
System.out.println("不相同");
}
}
}