Java 重新实现 ==

发布于 2024-10-07 14:03:20 字数 203 浏览 0 评论 0原文

例如,我知道,在 Python 中,如果我重新实现方法 __ cmp __ 我正在修改 == 的行为。我认为在Java中可以做同样的事情,重新实现equals(就像,当你重新实现toString时,它会影响打印),但是......不,或者,我不知道如何(我在谷歌中搜索,似乎那,你不能)我对吗? equals不影响==??如果是这样,平等的意义何在? 谢谢

I know, for instance that, in Python, if I reimplement the method __ cmp __ I am modifying the behaviour of ==. I thought that the same thing could be done in Java, reimplementing equals (like, when you reimplement toString, it affects the print), but... No, or, I don't know how (I searched in google and it seems that, you couldn't) Am I right?? equals does not affect the ==?? If so, what's the point of equals??
Thanks

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

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

发布评论

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

评论(5

大姐,你呐 2024-10-14 14:03:20

Python 的 == 运算符与 Java 中的 .equals() 相同。您可以在 Python 中使用 .__cmp__() 和 Java 中的 .equals() 覆盖它。

Python 的 is 运算符与 Java 中的 == 相同,并且都不能被覆盖。

Python's == operator is the same as .equals() in Java. You can override this with .__cmp__() in Python and .equals() in Java.

Python's is operator is the same as == in Java, and neither of these can be overridden.

2024-10-14 14:03:20

运算符== 比较对象引用是否相等。 equals 方法旨在执行比较——例如,如果满足以下条件,表示相同字符序列的两个不同的String对象将比较相等:您可以使用equals,但如果您使用==则不会。

据我所知,Java 中出于语言设计的考虑而忽略了运算符重载。 (为什么语言设计者在 String 上构建了 + 的重载,这让我很困惑。是的,很方便,但在我看来,这是作弊。)

The operator == compares object references for equality. The equals method is intended to perform value comparison -- for example, two distinct String objects that represent the same sequence of characters will compare equal if you use equals, but not if you use ==.

As far as I know, operator overloading was left out of Java as a matter of language design. (Why the language designers built in an overload for + over String boggles my mind. Convenient, yes, but IMO that's cheating.)

心如狂蝶 2024-10-14 14:03:20

== 是一个比较器

.equals() 是一个方法

需要 equals 是因为比较对象并不像进行简单比较那么直接。

如果你说 object1 == object2

那么,只有当它们指向相同的内存空间(即它们引用相同的对象)时,结果才为真。

但是,如果您想检查对象的属性,甚至属性的子集是否相同,那么您将实现自己的 equals 方法,并指定两个对象相等的构成。

那么,答案是,你定义什么是平等?

  • 两个对象在内存中引用同一个对象?然后使用 ==
  • 或两个包含相同数据的对象。然后使用 .equals()

== is a comparator

.equals() is a method

The need for equals is because comparing objects is not as straight forward as doing a simple comparison.

If you say object1 == object2

Then, the result is only true if they point to the same memory space (i.e. they reference the SAME object).

If however, you want to check that the attributes, or even a subset of attributes of an object are the same, then you would implement your own equals method, and specify what constitutes two objects being equal.

So, the answer is, what do you define as equal?

  • Two objects that reference the same object in memory? Then use ==
  • or two objects that contain the same data. Then use .equals()
清眉祭 2024-10-14 14:03:20

== 比较引用,而不是值。使用 == 与对象引用通常仅限于以下情况:

  1. 比较引用是否是
    无效的。
  2. 比较两个枚举值。这是可行的,因为只有一个
    每个枚举常量的对象。
  3. 您想知道两个引用是否对同一个对象

a.equals(b) 比较值是否相等。由于此方法是在 Object 类中定义的,所有其他类都是从该类派生的,因此它会自动为每个类定义。但是,它不会对大多数类执行智能比较,除非该类覆盖它。对于大多数 Java 核心类来说,它是以有意义的方式定义的。如果没有为(用户)类定义它,则其行为与 == 相同。

== compares references, not values. The use of == with object references is generally limited to the following:

  1. Comparing to see if a reference is
    null.
  2. Comparing two enum values. This works because there is only one
    object for each enum constant.
  3. You want to know if two references are to the same object

a.equals(b) compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.

坠似风落 2024-10-14 14:03:20

这是不可能的。这是 Java 没有继承的一项 C++ 功能......

It can't be done. That's one C++ feature Java didn't inherited...

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