为什么答案是B
class Person{
private String name,comment;
private int age;
public Person(String n,int a,String c){
name=n;age=a;comment=c;
}
public boolean equals(Object o){
if(!(o instanceOf Person)) return false;
Person p=(Person)o;
return age==p.age&&name.equals(p.name);
}
}
what is the appropriated definition of the hashCode method in class Person?
A.return super.hashCode();
B.return super.hashCode()+age*7;
c.return super.hashCode()+comment.hashCode()/2;
D.return super.hashCode()+comment.hashCode()/2-age*3;
------解决方案--------------------I don 't know.
------解决方案--------------------Well, you have to study the relationship between hashCode() and equals() methods.
------解决方案--------------------读一下Effective Java,equals方法里没有用到comment故排除c,d,a的话会产生相同的hashcode,降低散列表查找的效率.
如果两个对象equals为真,那hashcode一定要一样,如果两个对象equals为假,hashcode可以一样,但尽量不一样,可以提高效率