gpt4 book ai didi

java - 为什么这两个简单对象不相等?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:53 24 4
gpt4 key购买 nike

我有一个学校类(class):

public class School {

private String name;
private int id;
private boolean isOpen;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) {
this.isOpen = isOpen;
}
}

然后我创建了两个School实例,并比较了两个实例的相等性:

public static void main(String[] args) {
//school1
School school1 = new School();
school1.setId(1);
school1.setName("schoolOne");

//school2
School school2 = new School();
school2.setId(1);
school2.setName("schoolOne");

//result is false , why?
System.out.println("school1 == school2 ? " + school1.equals(school2));

}

即使我将相同的 idname 设置为 school1school2 实例,但是 school1.equals(school2) 返回 false,为什么?

最佳答案

您必须覆盖 equals(Object)方法:

把这个放在你的学校类(class)里:

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null || !(other instanceof School)) return false;
School school = (School) other;
if (school.id != this.id) return false;
if (!(school.name.equals(this.name))) return false;
if (school.isOpen != this.isOpen) return false;
if (!(school.hashCode().equals(this.hashCode()))) return false;
return true;
}

如果你打算这样做,覆盖 hashCode() 也是明智的方法也是如此。

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) id;
result = prime * result + (name != null ? name.hashCode() : 0);
result = prime * result + (isOpen ? 0 : 1);
return result;
}

附加信息

我相信这是覆盖 hashCode() 的最佳解释。

此答案由 dmeister 发布对于以下帖子:SO: Hash Code implementation .

我一直引用这个,看起来这个功能在 Eclipse 中为给定类生成 hashCode() 方法时使用。

A for nearly all cases reasonable good implementation was proposed in Josh Bloch's "Effective Java" in item 8. The best thing is to look it up there because the author explains there why the approach is good.

A short version:

  1. Create a int result and assign a non-zero value.

  2. For every field tested in the equals-Method, calculate a hash code c by:

    • If the field f is a boolean: calculate (f ? 0 : 1);
    • If the field f is a byte, char, short or int: calculate (int)f;
    • If the field f is a long: calculate (int)(f ^ (f >>> 32));
    • If the field f is a float: calculate Float.floatToIntBits(f);
    • If the field f is a double: calculate Double.doubleToLongBits(f) and handle the return value like every long value;
    • If the field f is an object: Use the result of the hashCode() method or 0 if f == null;
    • If the field f is an array: See every field as separate element and calculate the hash value in a recursive fashion and combine the values as described next.

  3. Combine the hash value c with result with:

    result = 37 * result + c

  4. Return result

    This should result in a proper distribution of hash values for most use situations.

关于java - 为什么这两个简单对象不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com