Created
October 30, 2013 01:54
-
-
Save turbotree/7226038 to your computer and use it in GitHub Desktop.
两种重写equals的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
private String name; | |
private String code; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
@Override | |
public boolean equals(Object obj) {//电脑重写Object的equals方法; | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
User other = (User) obj; | |
if (code == null) { | |
if (other.code != null) | |
return false; | |
} else if (!code.equals(other.code)) | |
return false; | |
if (name == null) { | |
if (other.name != null) | |
return false; | |
} else if (!name.equals(other.name)) | |
return false; | |
return true; | |
} | |
//自己重写Object类中的equals方法; | |
/*public boolean equals(Object obj){ | |
if(obj==null){ | |
return false; | |
} | |
if(!(obj instanceof User)){ | |
return false; | |
} | |
User u=(User)obj; | |
if(!(this.getName().equals(u.getName()))){ | |
return false; | |
} | |
if(!(this.getCode().equals(u.getCode()))){ | |
return false; | |
} | |
return true; | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment