用户声明一个对象的时候,即:new一个对象的时候,会存储在堆区当中,不同的对象存储的堆区位置不同。因此对象之间比较的时候,不能单纯的直接比较,应该重写hashCode和equals方法
范例如下:
public class Account{
private String name;
public Account(){}
public Account(String name){
this.name=name;
}
public String getName() {return name;}public void setName(String name) {this.name = name;}
@Overridepublic int hashCode() {return name.hashCode();}@Overridepublic boolean equals(Object other) {if (other == null || other.getClass() != getClass()) {return false;}return name.equals(((Account) other).name);}
....
}