hashcode implementation based on the Object Class
hi guys i have a little problem here to solve.
I have created an equals() method which is only based on the class of the object so that if two obj are of the same class the obj1.equals(obj2) return true. now my question is, How do i implement a hashcode() based only on the obj Class ??
@Override
public boolean equals(Object obj){
if(obj.getClass() == this.getClass()){
return true;
}else{
return false;
}
}
You could return the hashCode
of the Class
instance:
public int hashCode ()
{
return getClass().hashCode();
}
This would ensure that two objects which are equal based on your equals()
implementation would have the same hashCode()
.
上一篇: Eclipse颜色主题和非
下一篇: 基于对象类的哈希码实现