正确重写 equals 方法

发布于 2024-11-16 05:32:46 字数 654 浏览 4 评论 0原文

我的老师给了我一个 equals 重写示例的解决方案,它是这样的:

@Override
public boolean equals(Object o)
{
    if (this == o) return true;
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}

该方法被 Product 类重写,它有一个对于每个产品都是唯一的属性 id。但我不明白第一个if的含义,我认为第二个if已经检查了第一个if的限制。谁能给我一个这个代码工作的例子,而下面这个代码不行?谢谢!

@Override
public boolean equals(Object o)
{
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}

my teacher gave me the solution of a equals overriding example and it goes like this:

@Override
public boolean equals(Object o)
{
    if (this == o) return true;
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}

the method is overrided for Product class,which have an attribute id which is unique for each product. But I don't understand the meaning of the first if, I think that the second if already check the restrictions of the first one. Can anyone give me an example of this code working, and this one below not? Thanks!

@Override
public boolean equals(Object o)
{
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

池予 2024-11-23 05:32:46

这两个代码示例都有效。第一个示例中的 if (this == o) return true; 是性能优化(很可能是过早优化 - 始终首先分析),它检查对象是否是与自身进行比较。在 Java 中,== 运算符比较两个对象是否是同一个实例,而不是比较它们是否是具有相同数据的不同实例。

equals 方法有多种编写风格。我通常是这样做的:

public boolean equals(Object obj) {
    if (obj instanceof Product) {
        Product that = (Product) obj;
        return this.id == that.id;
    }
    return false;
}

如果我知道我的代码永远不会将该对象与其他类型的对象或 null 进行比较,那么我什至可以编写如下所示的代码,以便无论如何它都会抛出异常发生 - 这意味着我的代码有一个错误,所以通过尽早失败,我会发现它并尽快修复它。

public boolean equals(Object obj) {
    Product that = (Product) obj;
    return this.id == that.id;
}

Both of the code examples work. The if (this == o) return true; in the first example is a performance optimization (most probably premature optimization - always profile first), which checks whether the object is being compared to itself. In Java the == operator compares whether two objects are the same instance, not whether they are different instances with the same data.

There are may styles of writing the equals method. Here is how I usually do it:

public boolean equals(Object obj) {
    if (obj instanceof Product) {
        Product that = (Product) obj;
        return this.id == that.id;
    }
    return false;
}

If I know that my code will never compare the object against objects of other types, or against null, then I may even write the code as shown below, so that it will throw an exception if that anyways happens - it would mean that my code has a bug, so by failing early I will find out about it and fix it sooner.

public boolean equals(Object obj) {
    Product that = (Product) obj;
    return this.id == that.id;
}
摇划花蜜的午后 2024-11-23 05:32:46

你是对的。第一个 if 语句是多余的,因为 this == o 意味着 o instanceof Productthis.id == other.id

如果争论的是性能,我会说它有点过早优化的味道。

You are right. The first if-statement is redundant since this == o implies o instanceof Product and this.id == other.id.

If the argument is performance, I'd say it smells premature optimization.

薄荷→糖丶微凉 2024-11-23 05:32:46
if (this == o) return true;

上述说法是多余的。

更具体地说,它只是检查您是否正在将对象与自身进行比较...因此它可以跳过其下面的代码。例如:

Foo f = new Foo();
f.equals(f); //the if (this == o) would be true. References the same thing.

注意:顺便说一句,如果一个人重写 equals,则应该重写 hashcode() 以维护 hashcode() 的一般契约 - 相等的对象必须具有相同的 hashcode(反之则不然,因为两个对象可以具有相同的 hashcode)散列但不相等。)

http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)

if (this == o) return true;

The above statement is redundant.

More specifically, it's simply checking to see if you are comparing an object to itself ... so it can skip the code below it. For example:

Foo f = new Foo();
f.equals(f); //the if (this == o) would be true. References the same thing.

Note: As an aside, if one overrides equals, one should override hashcode() to maintain the general contract for hashcode() - equal objects must have the same hashcode (the reverse is not true since two objects could have the same hash but not be equal.)

http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)

咋地 2024-11-23 05:32:46

在 Eclipse 中,您可以选择“生成 hashCode() 和 equals()...”(菜单源)

In eclipse you have the option to "Generate hashCode() and equals()..." (menu Source)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文