各位大神,在Person类中重写了equals方法和hashCode方法,hashcode方法的内部实现如代码所示,测试中,如果p1和p2的姓名和年龄完全相同,打印出来的地址值是一样的,但为什么p1==p2的结果是false呢???- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Student other = (Student) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
复制代码
|
|