重载java中的equals方法

发布于 2024-09-06 03:16:41 字数 422 浏览 2 评论 0原文

可能的重复:
关于 equals 的最佳实践:重载还是不重载?

有人重载java中的equals方法吗?重载方法将是

public boolean equals(final MyClass myClass)

这将具有在另一个方法中具有相关比较部分(方法的内部)的好处。详细信息在我的博客中。

编辑:这是一个真正的问题。博客条目的链接已删除。

Possible Duplicate:
Best practices regarding equals: to overload or not to overload?

Does anyone overload the equals method in java? The overloaded method will be

public boolean equals(final MyClass myClass)

This will have the benefit of having the relevant comparison part (guts of the method) in another method. Details are in my blog.

EDIIT : This is a genuine question. Link to the blog entry removed.

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

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

发布评论

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

评论(1

泪眸﹌ 2024-09-13 03:16:41

IMO,这将是一个坏主意,因为它很容易让阅读您代码的人感到困惑。我永远不会这样做。

如果有必要提供一个不重写 boolean equals(Object) 的“相等”方法,最好给它一个不同的方法名称。

IMO, it would be a bad idea because it would be liable to confuse someone reading your code. I'd never do it.

If it becomes necessary to provide an "equality" method that does not override boolean equals(Object), it is a much better idea to give it a different method name.

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