public boolean equals(Object o){
if(this==0) return true;
if(! (o instanceof ObjectChild)) return false;
final ObjectChild other = (ObjectChild) o ;
if(this.name.equals(other.getName()) && this.age == other.getAge())
//这里假定的 ObjectChild 是一个JavaBean 对象,含有 name 和 age 2个属性,如果name和age相同,则看为同一个对象
return true;
else
return false;
}
ObjectChild覆盖Object类的hashCode()方法
//返回自定义方法计算出的hashCode值,使得拥有相同属性的对象拥有相同的hashCode;
public int hashCode(){
int result;
result = (name==null?0:name.hashCode());
result = 29*result + (age==null?0:age.hashCode());
return result;
}