超载是否等于值得

发布于 2024-09-02 14:06:49 字数 2287 浏览 4 评论 0原文

考虑以下代码片段:

import java.util.*;
public class EqualsOverload {
    public static void main(String[] args) {
        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
        }
        List<Thing> myThings = Arrays.asList(new Thing(42));
        System.out.println(myThings.contains(new Thing(42))); // prints "false"
    }
}

请注意,contains 返回false!!!我们好像东西丢了!!

当然,这个错误是我们不小心重载,而不是覆盖Object.equals(Object)。如果我们按如下方式编写 class Thing,则 contains 按预期返回 true

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.x == ((Thing) o).x);
            }
        }

Effective Java 第 2 版,第 36 条:一致使用 Override 注释,使用本质上相同的参数来建议应一致使用 @Override。当然,这个建议很好,因为如果我们尝试在第一个片段中声明 @Override equals(Thing other),我们友好的小编译器会立即指出我们愚蠢的小错误,因为它是一个过载,而不是覆盖。

然而,本书没有具体讨论重载equals是否是一个好主意。本质上,有 3 种情况:

  • 仅重载,不覆盖 - 几乎肯定错误
    • 这本质上是上面的第一个片段
  • 仅覆盖(无重载)——一种修复方法
    • 这本质上是上面的第二个片段
  • - 另一种修复方法

第三种情况由以下片段说明:

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.equals((Thing) o));
            }
        }

这里,即使我们现在有 2 个 equals 方法,那里仍然是一种相等逻辑,并且它位于重载中。 @Override 只是委托给重载。

所以问题是:

  • “仅覆盖”与“过载和覆盖组合”的优缺点是什么?
  • 重载 equals 是否有理由,或者这几乎肯定是一种不好的做法?

Consider the following snippet:

import java.util.*;
public class EqualsOverload {
    public static void main(String[] args) {
        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
        }
        List<Thing> myThings = Arrays.asList(new Thing(42));
        System.out.println(myThings.contains(new Thing(42))); // prints "false"
    }
}

Note that contains returns false!!! We seems to have lost our things!!

The bug, of course, is the fact that we've accidentally overloaded, instead of overridden, Object.equals(Object). If we had written class Thing as follows instead, then contains returns true as expected.

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.x == ((Thing) o).x);
            }
        }

Effective Java 2nd Edition, Item 36: Consistently use the Override annotation, uses essentially the same argument to recommend that @Override should be used consistently. This advice is good, of course, for if we had tried to declare @Override equals(Thing other) in the first snippet, our friendly little compiler would immediately point out our silly little mistake, since it's an overload, not an override.

What the book doesn't specifically cover, however, is whether overloading equals is a good idea to begin with. Essentially, there are 3 situations:

  • Overload only, no override -- ALMOST CERTAINLY WRONG!
    • This is essentially the first snippet above
  • Override only (no overload) -- one way to fix
    • This is essentially the second snippet above
  • Overload and override combo -- another way to fix

The 3rd situation is illustrated by the following snippet:

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.equals((Thing) o));
            }
        }

Here, even though we now have 2 equals method, there is still one equality logic, and it's located in the overload. The @Override simply delegates to the overload.

So the questions are:

  • What are the pros and cons of "override only" vs "overload & override combo"?
  • Is there a justification for overloading equals, or is this almost certainly a bad practice?

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

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

发布评论

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

评论(8

蓝海似她心 2024-09-09 14:06:49

我没有看到重载 equals 的情况,除了它更容易出错并且更难维护,特别是在使用继承时。

在这里,维护自反性、对称性和传递性或检测它们的不一致可能非常困难,因为您始终必须了解所调用的实际 equals 方法。想象一下一个大的继承层次结构,并且只有一些类型实现了自己的重载方法。

所以我想说的是不要这样做。

I'dont see the case for overloading equals, except that is more error-prone and harder to maintain, especially when using inheritance.

Here, it can be extremly hard to maintain reflexivity, symmetry and transitivity or to detect their inconsistencies, because you always must be aware of the actual equals method that gets invoked. Just think of a large inheritance hierarchie and only some of the types implementing their own overloading method.

So I'd say just don't do it.

慢慢从新开始 2024-09-09 14:06:49

如果您只有一个字段(如您的示例所示),我认为

@Override public boolean equals(Object o) {
    return (o instanceof Thing) && (this.x == ((Thing) o).x);
}

这是一种可行的方法。在我看来,其他任何事情都会过于复杂。但是如果你添加一个字段(并且不想通过 sun 的 80 列建议),它看起来会比

@Override public boolean equals(Object o) {
    if (!(o instanceof Thing))
        return false;
    Thing t = (Thing) o;
    return this.x == t.x && this.y == t.y;
}

我认为稍微难看

public boolean equals(Thing o) {
    return this.x == o.x && this.y == o.y;
}

@Override public boolean equals(Object o) {
    // note that you don't need this.equals().
    return (o instanceof Thing) && equals((Thing) o);
}

所以我的经验法则基本上是,如果需要多次投射它在仅覆盖中,执行覆盖/过载组合


次要方面是运行时开销。正如 Java 性能编程,第 2 部分:铸造成本解释:

向下转换操作(在 Java 语言规范中也称为缩小转换)将祖先类引用转换为子类引用。此转换操作会产生执行开销,因为 Java 要求在运行时检查转换以确保其有效。

通过使用重载/覆盖组合,在某些情况下(不是全部!),编译器将设法避免向下转型。


要评论@Snehal 的观点,公开这两种方法可能会让客户端开发人员感到困惑:另一种选择是让重载的 equals 为私有。优雅性得以保留,该方法可以在内部使用,而客户端的接口看起来与预期一致。

If you have one single field as in your example, I think

@Override public boolean equals(Object o) {
    return (o instanceof Thing) && (this.x == ((Thing) o).x);
}

is the way to go. Anything else would be overly complicated imo. But if you add a field (and don't want to pass the 80-column recommendation by sun) it would look something like

@Override public boolean equals(Object o) {
    if (!(o instanceof Thing))
        return false;
    Thing t = (Thing) o;
    return this.x == t.x && this.y == t.y;
}

which I think is slightly uglier than

public boolean equals(Thing o) {
    return this.x == o.x && this.y == o.y;
}

@Override public boolean equals(Object o) {
    // note that you don't need this.equals().
    return (o instanceof Thing) && equals((Thing) o);
}

So my rule of thumb is basically, if need to cast it more than once in override-only, do the override-/overload-combo.


A secondary aspect is the runtime overhead. As Java performance programming, Part 2: The cost of casting explains:

Downcast operations (also called narrowing conversions in the Java Language Specification) convert an ancestor class reference to a subclass reference. This casting operation creates execution overhead, since Java requires that the cast be checked at runtime to make sure that it's valid.

By using the overload-/override-combo, the compiler will, in some cases (not all!) manage to do without the downcast.


To comment on @Snehal point, that exposing both methods possibly confuses client-side developers: Another option would be to let the overloaded equals be private. The elegance is preserved, the method can be used internally, while the interface to the client side looks as expected.

谁把谁当真 2024-09-09 14:06:49

重载等于的问题:

  • Java ie 提供的所有集合; Set、List、Map 使用重写的方法来比较两个对象。所以即使重载了equals方法,也没有解决比较两个对象的目的。另外,如果你只是重载并实现 hashcode 方法,则会导致错误的行为

  • 如果您同时重载和重写 equals 方法并公开这两种方法,您将使客户端开发人员感到困惑。按照惯例,人们认为您正在重写 Object 类

Issues with Overloaded Equals:

  • All the Collections provided by Java ie; Set, List, Map use the overridden method for comparing two objects. So even if you overload the equals method, it doesn't solve the purpose of comparing two objects. Also, if you just overload and implement the hashcode method, it would result in erroneous behavior

  • If you have both overloaded and overridden equals methods and exposing both these methods you are going to confuse the client side developers. It is by convention people believe that you are overriding the Object class

迷迭香的记忆 2024-09-09 14:06:49

书中有很多内容涉及这一点。 (它不在我面前,所以我将参考我记得的项目)

有一个完全使用 equals(..) 的例子,据说不应使用重载,并且如果使用 - 应小心使用。关于方法设计的条目警告不要重载具有相同数量参数的方法。所以 - 不,不要重载 equals(..)

更新: 来自“Effective Java” (p.44)

除了正常的方法之外,提供这样一个“强类型”的 equals 方法是可以接受的,只要这两个方法返回相同的结果,但没有令人信服的理由这样做。

因此,不禁止这样做,但它会增加类的复杂性,同时不会增加任何收益。

There are a number of items in the book that cover this. (It's not in front of me, so I'll refer to items as I remember them)

There is en example exactly using equals(..) where it is said that overloading should not be used, and if used - it should be used with care. The item about method design warns against overloading methods with the same number of arguments. So - no, don't overload equals(..)

Update: From "Effective Java" (p.44)

It is acceptable to provide such a "strongly typed" equals method in addition to the normal one as long as the two methods return the same result, but there is no compelling reason to do so.

So, it is not forbidden to do so, but it adds complexity to your class, while adding no gains.

拍不死你 2024-09-09 14:06:49

我在项目中使用这种方法与覆盖和重载组合,因为代码看起来更干净一些。到目前为止,我对这种方法没有遇到任何问题。

I use this approach with override and overload combo in my projects, because code looks a bit cleaner. I didn't have problems with this approach so far.

像极了他 2024-09-09 14:06:49

让我分享一个带有 Overloaded equals 的“错误代码”示例:

class A{
    private int val;

    public A(int i){
        this.val = i;
    }

    public boolean equals(A a){
        return a.val == this.val;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this.val);
    }
}

public class TestOverloadEquals {

    public static void main(String[] args){
        A a1 = new A(1), a2 = new A(2);
        List<A> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        A a3 =  new A(1);

        System.out.println(list.contains(a3));
    }
}

Let me share an example of "buggy code" with Overloaded equals:

class A{
    private int val;

    public A(int i){
        this.val = i;
    }

    public boolean equals(A a){
        return a.val == this.val;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this.val);
    }
}

public class TestOverloadEquals {

    public static void main(String[] args){
        A a1 = new A(1), a2 = new A(2);
        List<A> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        A a3 =  new A(1);

        System.out.println(list.contains(a3));
    }
}
彩扇题诗 2024-09-09 14:06:49

我可以想到一个非常简单的示例,其中这将无法正常工作,以及为什么您永远不应该这样做:

class A {
   private int x;

   public A(int x) {
       this.x = x;
   }

   public boolean equals(A other) {
       return this.x == other.x;
   }

   @Override
   public boolean equals(Object other) {
       return (other instanceof A) && equals((A) other);
   }
}

class B extends A{
    private int y;

    public B(int x, int y) {
        super(x);
        this.y = y;
    }

    public boolean equals(B other) {
        return this.equals((A)other) && this.y == other.y; 
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof B) && equals((B) other);
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new B(1,1);
        B b1 = new B(1,1);
        B b2 = new B(1,2);

        // This obviously returns false
        System.out.println(b1.equals(b2));
        // What should this return? true!
        System.out.println(a.equals(b2));
        // And this? Also true!
        System.out.println(b2.equals(a));
    }
}

在此测试中,您可以清楚地看到使用继承时重载方法弊大于利。在这两种错误的情况下,都会调用更通用的 equals(A a) ,因为 Java 编译器只知道 aA 类型,并且对象没有重载的 equals(B b) 方法。

事后思考:将重载的equals设为私有确实解决了这个问题,但这真的对你有好处吗?它只是添加了一个额外的方法,该方法只能通过强制转换来调用。

I can think of a very simple example where this won't work properly and why you should never do this:

class A {
   private int x;

   public A(int x) {
       this.x = x;
   }

   public boolean equals(A other) {
       return this.x == other.x;
   }

   @Override
   public boolean equals(Object other) {
       return (other instanceof A) && equals((A) other);
   }
}

class B extends A{
    private int y;

    public B(int x, int y) {
        super(x);
        this.y = y;
    }

    public boolean equals(B other) {
        return this.equals((A)other) && this.y == other.y; 
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof B) && equals((B) other);
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new B(1,1);
        B b1 = new B(1,1);
        B b2 = new B(1,2);

        // This obviously returns false
        System.out.println(b1.equals(b2));
        // What should this return? true!
        System.out.println(a.equals(b2));
        // And this? Also true!
        System.out.println(b2.equals(a));
    }
}

In this test, you can clearly see that the overloaded method does more harm than good when using inheritance. In both wrong cases the more generic equals(A a) is called, because the Java compiler only knows that a is of type A and that object does not have the overloaded equals(B b) method.

Afterthought: making the overloaded equals private does solve this problem, but was does that really gain you? It only adds an extra method, which can only be called by doing a cast.

昵称有卵用 2024-09-09 14:06:49

由于这个问题已有 10 年历史,原作者可能对答案不再感兴趣,因此我将添加一些对当今寻求答案的开发人员有用的信息。

对于 Android 开发人员来说,Android Studio 包含一个模板,当尝试重载等于运算符时会弹出该模板。它还生成一个正确的 hashCode 方法,并且生成的重写的 equals 方法将如下所示:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Thing thing = (Thing) o;
    return x.equals(thing.x);
}

As this question is 10 years old and the original author is probably not interested in the answer anymore, I'll add some information that can be useful to developers looking for an answer nowadays.

For Android developers, Android Studio contains a template that will pop-up when trying to overload the equal operator. It also generates a proper hashCode method, and the resulting overridden equals method would look like this:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Thing thing = (Thing) o;
    return x.equals(thing.x);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文