public class Dog
{
int collarID;
String name;
public static void main(String[] args){
Dog d = new Dog();
d.name="hose";
System.out.print(d.hashCode());
}
public boolean equals(Object arg0)
{
if (arg0 instanceof Dog)
{
Dog new_name = (Dog) arg0;
return collarID==new_name.collarID && new_name.name.equals(name);
}
return false;
}
public int hashCode()
{
return toString().length();//StackOverflow
}
}
我错过了什么?由于默认的 toString() 方法,是否会经常调用 hashCode() 方法?