class A{

@Override
public int hashCode() {
// TODO Auto-generated method stub
return 1;
}
        public static void main(String[] args) {
System.out.println(new A().equals(new B()));//false 这里为什么还是false??
System.out.println(new A().hashCode()==new B().hashCode());//true
}

}
class B{

@Override
public int hashCode() {
// TODO Auto-generated method stub
return 1;
}
}

解决方案 »

  1.   

    重写hashCode,不跟equals相同。你需要重载equals方法,才可以呢。
      

  2.   

        public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) { // 判断目标对象是否为String
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = offset;
    int j = anotherString.offset;
    while (n-- != 0) {
        if (v1[i++] != v2[j++])
    return false;
    }
    return true;
        }
    }
    return false;
        }这是String的equals方法,它判断了两个对象是不是同一个对象,如果不是,直接返回false