超载是否等于值得
考虑以下代码片段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我没有看到重载 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.
如果您只有一个字段(如您的示例所示),我认为
这是一种可行的方法。在我看来,其他任何事情都会过于复杂。但是如果你添加一个字段(并且不想通过 sun 的 80 列建议),它看起来会比
我认为稍微难看
所以我的经验法则基本上是,如果需要多次投射它在仅覆盖中,执行覆盖/过载组合。
次要方面是运行时开销。正如 Java 性能编程,第 2 部分:铸造成本解释:
通过使用重载/覆盖组合,在某些情况下(不是全部!),编译器将设法避免向下转型。
要评论@Snehal 的观点,公开这两种方法可能会让客户端开发人员感到困惑:另一种选择是让重载的 equals 为私有。优雅性得以保留,该方法可以在内部使用,而客户端的接口看起来与预期一致。
If you have one single field as in your example, I think
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
which I think is slightly uglier than
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:
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.
重载等于的问题:
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
书中有很多内容涉及这一点。 (它不在我面前,所以我将参考我记得的项目)
有一个完全使用
equals(..)
的例子,据说不应使用重载,并且如果使用 - 应小心使用。关于方法设计的条目警告不要重载具有相同数量参数的方法。所以 - 不,不要重载equals(..)
更新: 来自“Effective Java” (p.44)
因此,不禁止这样做,但它会增加类的复杂性,同时不会增加任何收益。
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 overloadequals(..)
Update: From "Effective Java" (p.44)
So, it is not forbidden to do so, but it adds complexity to your class, while adding no gains.
我在项目中使用这种方法与覆盖和重载组合,因为代码看起来更干净一些。到目前为止,我对这种方法没有遇到任何问题。
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.
让我分享一个带有 Overloaded equals 的“错误代码”示例:
Let me share an example of "buggy code" with Overloaded equals:
我可以想到一个非常简单的示例,其中这将无法正常工作,以及为什么您永远不应该这样做:
在此测试中,您可以清楚地看到使用继承时重载方法弊大于利。在这两种错误的情况下,都会调用更通用的
equals(A a)
,因为 Java 编译器只知道a
是A
类型,并且对象没有重载的 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:
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 thata
is of typeA
and that object does not have the overloadedequals(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.由于这个问题已有 10 年历史,原作者可能对答案不再感兴趣,因此我将添加一些对当今寻求答案的开发人员有用的信息。
对于 Android 开发人员来说,Android Studio 包含一个模板,当尝试重载等于运算符时会弹出该模板。它还生成一个正确的 hashCode 方法,并且生成的重写的 equals 方法将如下所示:
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: