什么时候应该覆盖Equals功能? [重复]

时间:2022-09-10 23:19:11

Possible Duplicate:
Java: Always override equals?

可能重复:Java:始终覆盖等于?

should I override equals function for any class that I create?

我应该为我创建的任何类重写equals函数吗?

even for very simple classes which only contains some very simple attributes and by equals I need every single attribute of it to be equal?

即使是非常简单的类,只包含一些非常简单的属性,并且通过equals我需要它的每个属性相等?

3 个解决方案

#1


30  

should I override equals function for any class that I create?

我应该为我创建的任何类重写equals函数吗?

Override equals if (and only if) the object "represents some data", i.e. if it models something such as Person, Car or RecipieIngredient (these typically end up in collections etc). Don't override equals for other types of classes, for example LoginServlet or DatabaseUtil.

覆盖等于if(并且仅当)对象“代表某些数据”,即,如果它模拟诸如Person,Car或RecipieIngredient之类的东西(这些通常最终在集合中等)。不要为其他类型的类重写equals,例如LoginServlet或DatabaseUtil。

Remember to always override hashCode whenever you override equals.

请记住,只要覆盖equals,就始终覆盖hashCode。

(A natural follow-up question:) What happens if I don't override equals and hashCode?

(一个自然的后续问题:)如果我不重写equals和hashCode会发生什么?

Any two objects will be considered unequal unless they are the exact same object.

任何两个对象都将被视为不相等,除非它们是完全相同的对象。

[...] I need every single attribute of it to be equal?

[...]我需要它的每一个属性是平等的吗?

Typically yes. It depends on how you define your notion of equality. Note that for reference types, you can reuse/delegate to that objects implementation of equals (and hashCode) when implementing your own.

通常是的。这取决于你如何定义你的平等概念。请注意,对于引用类型,您可以在实现自己的对象时重用/委托equals(和hashCode)的对象实现。

Related questions:

相关问题:

#2


16  

You should only override equals() if you have a reason to do so. As described here, it is very difficult to write a proper equals() method for non-final or mutable classes.

如果你有理由,你应该只覆盖equals()。如此处所述,为非final或可变类编写适当的equals()方法非常困难。

If your application requires some sort of equality concept that is different from "the identical object", then by all means go ahead. Just read the above reference to be aware of what's involved. But as a matter of routine? Definitely not.

如果您的应用程序需要某种与“相同对象”不同的平等概念,那么请务必继续。只需阅读上述参考资料即可了解所涉及的内容。但作为例行公事?当然不。

#3


2  

Well, the concept of over-riding equals() is more understood if you think it in real-life terms.

好吧,如果你用现实生活方式来思考,那么更多地理解覆盖等于()的概念。

Equals method should be over-ridden only when two objects need to be equal logically. Also, if you fear, somewhere in your programme an object may be re-created, then you must over-ride equals().

只有当逻辑上两个对象需要相等时,才应该覆盖Equals方法。此外,如果你担心,程序中的某个地方可能会重新创建一个对象,那么你必须重叠equals()。

A very good example is String objects in java.

一个很好的例子是java中的String对象。

   String string1= new String("hello");

   String string2= "hello";

Are they equal?.. yes absolutely they are..logically though. You are able to check their equality only because, Java has already over-ridden String equals() method.

它们是否相同?是的,但绝对是......但是。您只能检查它们的相等性,因为Java已经过度使用了String equals()方法。

As we all know, a class is actually a template for its objects. So let's consider, there is an employee class, which actually determines what attributes employees in the company may hold and actual employees in the company are objects of this class. So typically, attributes of an employee may be as follows.

众所周知,类实际上是其对象的模板。因此,让我们考虑一下,有一个员工类,它实际上决定了公司员工可能拥有的属性,公司中的实际员工是该类的对象。通常,员工的属性可以如下。

1.Employee Name

1.员工姓名

2.Employee ID

2.员工ID

3.Date Of Birth . . . .. . . . . . .

3.出生日期。 。 。 .. 。 。 。 。 。

So in this case , you should just check whether Employee ID is equal or not in your equals method. But yeah, if your objects lacks such distinct attribute, then you should go ahead and check almost all values to avoid make your programme consider two diffrent people equal.

因此,在这种情况下,您应该只检查您的equals方法中的员工ID是否相等。但是,如果你的对象缺乏这样明显的属性,那么你应该继续检查几乎所有的值,以避免让你的程序认为两个不同的人相等。

#1


30  

should I override equals function for any class that I create?

我应该为我创建的任何类重写equals函数吗?

Override equals if (and only if) the object "represents some data", i.e. if it models something such as Person, Car or RecipieIngredient (these typically end up in collections etc). Don't override equals for other types of classes, for example LoginServlet or DatabaseUtil.

覆盖等于if(并且仅当)对象“代表某些数据”,即,如果它模拟诸如Person,Car或RecipieIngredient之类的东西(这些通常最终在集合中等)。不要为其他类型的类重写equals,例如LoginServlet或DatabaseUtil。

Remember to always override hashCode whenever you override equals.

请记住,只要覆盖equals,就始终覆盖hashCode。

(A natural follow-up question:) What happens if I don't override equals and hashCode?

(一个自然的后续问题:)如果我不重写equals和hashCode会发生什么?

Any two objects will be considered unequal unless they are the exact same object.

任何两个对象都将被视为不相等,除非它们是完全相同的对象。

[...] I need every single attribute of it to be equal?

[...]我需要它的每一个属性是平等的吗?

Typically yes. It depends on how you define your notion of equality. Note that for reference types, you can reuse/delegate to that objects implementation of equals (and hashCode) when implementing your own.

通常是的。这取决于你如何定义你的平等概念。请注意,对于引用类型,您可以在实现自己的对象时重用/委托equals(和hashCode)的对象实现。

Related questions:

相关问题:

#2


16  

You should only override equals() if you have a reason to do so. As described here, it is very difficult to write a proper equals() method for non-final or mutable classes.

如果你有理由,你应该只覆盖equals()。如此处所述,为非final或可变类编写适当的equals()方法非常困难。

If your application requires some sort of equality concept that is different from "the identical object", then by all means go ahead. Just read the above reference to be aware of what's involved. But as a matter of routine? Definitely not.

如果您的应用程序需要某种与“相同对象”不同的平等概念,那么请务必继续。只需阅读上述参考资料即可了解所涉及的内容。但作为例行公事?当然不。

#3


2  

Well, the concept of over-riding equals() is more understood if you think it in real-life terms.

好吧,如果你用现实生活方式来思考,那么更多地理解覆盖等于()的概念。

Equals method should be over-ridden only when two objects need to be equal logically. Also, if you fear, somewhere in your programme an object may be re-created, then you must over-ride equals().

只有当逻辑上两个对象需要相等时,才应该覆盖Equals方法。此外,如果你担心,程序中的某个地方可能会重新创建一个对象,那么你必须重叠equals()。

A very good example is String objects in java.

一个很好的例子是java中的String对象。

   String string1= new String("hello");

   String string2= "hello";

Are they equal?.. yes absolutely they are..logically though. You are able to check their equality only because, Java has already over-ridden String equals() method.

它们是否相同?是的,但绝对是......但是。您只能检查它们的相等性,因为Java已经过度使用了String equals()方法。

As we all know, a class is actually a template for its objects. So let's consider, there is an employee class, which actually determines what attributes employees in the company may hold and actual employees in the company are objects of this class. So typically, attributes of an employee may be as follows.

众所周知,类实际上是其对象的模板。因此,让我们考虑一下,有一个员工类,它实际上决定了公司员工可能拥有的属性,公司中的实际员工是该类的对象。通常,员工的属性可以如下。

1.Employee Name

1.员工姓名

2.Employee ID

2.员工ID

3.Date Of Birth . . . .. . . . . . .

3.出生日期。 。 。 .. 。 。 。 。 。

So in this case , you should just check whether Employee ID is equal or not in your equals method. But yeah, if your objects lacks such distinct attribute, then you should go ahead and check almost all values to avoid make your programme consider two diffrent people equal.

因此,在这种情况下,您应该只检查您的equals方法中的员工ID是否相等。但是,如果你的对象缺乏这样明显的属性,那么你应该继续检查几乎所有的值,以避免让你的程序认为两个不同的人相等。