日期:2014-05-20 浏览次数:20985 次
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; } }
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; }