Java 重写 Object equals() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2316220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java override Object equals() method
提问by user69514
How do I override the equals method in the object class?
如何覆盖对象类中的 equals 方法?
i.e I have
即我有
class Person{
//need to override here
public boolean equals (Object obj){
}
I want to convert the parameter obj to a type Person, but if I do (Person) obj it won't work.
我想将参数 obj 转换为 Person 类型,但是如果我这样做 (Person) obj 它将不起作用。
采纳答案by pgmura
You can cast it inside the method, just make sure that is of the right type using instance of
您可以在方法中强制转换它,只需使用实例确保它是正确的类型
if(obj instanceof Person)
{
Person otherPerson = (Person) obj;
//Rest of the code to check equality
}
else
{
//return false maybe
}
回答by Dan Passaro
It's actually more complicated than you might think. Have Eclipse (or whatever IDE you're using) auto-generate an equals
method; you'll see it contains a few checks and casts before it does a comparison.
它实际上比你想象的要复杂。让 Eclipse(或您使用的任何 IDE)自动生成一个equals
方法;在进行比较之前,您会看到它包含一些检查和强制转换。
Also see here: http://www.javapractices.com/topic/TopicAction.do?Id=17
另见此处:http: //www.javapractices.com/topic/TopicAction.do?Id=17
回答by twodayslate
@Override
public boolean equals(Object o)
{
if (o instanceof Person)
{
Person c = (Person) o;
if ( this.FIELD.equals(c.FIELD) ) //whatever here
return true;
}
return false;
}
回答by cletus
Take a look at Regarding Object Comparison.
看看关于对象比较。
Be aware that if you override equals()
you mustalso override hashCode()
. The equals/hashCode contract is that if two objects are equal they must have the same hash code.
请注意,如果您覆盖,equals()
您还必须覆盖hashCode()
. equals/hashCode 约定是,如果两个对象相等,则它们必须具有相同的哈希码。
回答by saugata
If you plan to create subclasses of Person, use something like
如果您打算创建 Person 的子类,请使用类似
if(obj!=null && obj.getClass() == Person.class)
if(obj!=null && obj.getClass() == Person.class)
rather than instanceof
而不是实例
回答by jwenting
The only reason to use getClass()
rather than instanceof
is if one wanted to assert that both references being compared point to objects of the exact same class rather than objects implementing the same base class.
使用getClass()
而不是的唯一原因instanceof
是如果想要断言被比较的两个引用都指向完全相同的类的对象,而不是实现相同基类的对象。
Say we have an Employee
eand a Manager
m(extends Employee
).
假设我们有一个Employee
e和一个Manager
m(扩展Employee
)。
m instanceof Employee
would yield true, m.getClass() == Employee.class
would return false.
m instanceof Employee
会产生真,m.getClass() == Employee.class
会返回假。
In some cases the latter might be preferred, but rarely in case of comparison of instances in equals()
or hashCode()
methods.
在某些情况下,后者可能是首选,但在比较实例equals()
或hashCode()
方法的情况下很少。
回答by lucentmind
One more point may be good to know that after you override equals()
method (and also hashcode()
) method you can to compare two objects of same class like follows:
还有一点可能很好知道,在您覆盖equals()
方法(以及hashcode()
)方法之后,您可以比较同一类的两个对象,如下所示:
Person p1 = new Person();
Person p2 = new Person();
....
if ( p1.equals( p2 ) )
{
// --- Two Persons are equal, w.r.t the fields you specified in equals method ---
}
回答by Chad Bingham
I know this is answered, but in my travels I have found this the most efficient way to override the comparison of an object to make sure it happens the same globally:
我知道这得到了回答,但在我的旅行中,我发现这是覆盖对象比较以确保它在全局范围内发生相同的最有效方法:
@Override
public boolean equals(Object o) {
return o instanceof Person && this.getSomeMagicalField().equals(((Person) o).getSomeMagicalField());
}
orif you are not comparing strings:
或者如果您不比较字符串:
@Override
public boolean equals(Object o) {
return o instanceof Person && this.getSomeMagicalField() == (Person) o).getSomeMagicalField();
}
回答by wrapperapps
I prefer the simpler, null-safe(r) Objects.equals
for any field type:
Objects.equals
对于任何字段类型,我更喜欢更简单的空安全(r):
@Override
public boolean equals(Object o) {
if (o instanceof Person) {
Person p = (Person) o;
return Objects.equals(p.FIELD, this.FIELD);
}
return false;
}