为什么重写了equals()两类的对象还是不相等
public class phone1 { int size = 1; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + size; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; phone1 other = (phone1) obj; if (size != other.size) return false; return true; } }
public class phone2 { int size = 1; }
public class PHONE { public static void main(String[] args) { // TODO Auto-generated method stub phone1 p1 = new phone1(); phone2 p2 = new phone2(); if(p1.equals(p2)) { System.out.println("两个电脑的参数相同!"); } else { System.out.println("两个电脑的参数不同!"); } } }
结果是 两个电脑的参数不同!