正确重写 equals 方法
我的老师给了我一个 equals 重写示例的解决方案,它是这样的:
@Override
public boolean equals(Object o)
{
if (this == o) return true;
boolean result = false;
if (o instanceof Product)
{
Product other = (Product)o;
result = this.id == other.id;
}
return result;
}
该方法被 Product 类重写,它有一个对于每个产品都是唯一的属性 id。但我不明白第一个if的含义,我认为第二个if已经检查了第一个if的限制。谁能给我一个这个代码工作的例子,而下面这个代码不行?谢谢!
@Override
public boolean equals(Object o)
{
boolean result = false;
if (o instanceof Product)
{
Product other = (Product)o;
result = this.id == other.id;
}
return result;
}
my teacher gave me the solution of a equals overriding example and it goes like this:
@Override
public boolean equals(Object o)
{
if (this == o) return true;
boolean result = false;
if (o instanceof Product)
{
Product other = (Product)o;
result = this.id == other.id;
}
return result;
}
the method is overrided for Product class,which have an attribute id which is unique for each product. But I don't understand the meaning of the first if, I think that the second if already check the restrictions of the first one. Can anyone give me an example of this code working, and this one below not? Thanks!
@Override
public boolean equals(Object o)
{
boolean result = false;
if (o instanceof Product)
{
Product other = (Product)o;
result = this.id == other.id;
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这两个代码示例都有效。第一个示例中的
if (this == o) return true;
是性能优化(很可能是过早优化 - 始终首先分析),它检查对象是否是与自身进行比较。在 Java 中,==
运算符比较两个对象是否是同一个实例,而不是比较它们是否是具有相同数据的不同实例。equals 方法有多种编写风格。我通常是这样做的:
如果我知道我的代码永远不会将该对象与其他类型的对象或 null 进行比较,那么我什至可以编写如下所示的代码,以便无论如何它都会抛出异常发生 - 这意味着我的代码有一个错误,所以通过尽早失败,我会发现它并尽快修复它。
Both of the code examples work. The
if (this == o) return true;
in the first example is a performance optimization (most probably premature optimization - always profile first), which checks whether the object is being compared to itself. In Java the==
operator compares whether two objects are the same instance, not whether they are different instances with the same data.There are may styles of writing the equals method. Here is how I usually do it:
If I know that my code will never compare the object against objects of other types, or against null, then I may even write the code as shown below, so that it will throw an exception if that anyways happens - it would mean that my code has a bug, so by failing early I will find out about it and fix it sooner.
你是对的。第一个 if 语句是多余的,因为
this == o
意味着o instanceof Product
和this.id == other.id
。如果争论的是性能,我会说它有点过早优化的味道。
You are right. The first if-statement is redundant since
this == o
implieso instanceof Product
andthis.id == other.id
.If the argument is performance, I'd say it smells premature optimization.
上述说法是多余的。
更具体地说,它只是检查您是否正在将对象与自身进行比较...因此它可以跳过其下面的代码。例如:
注意:顺便说一句,如果一个人重写 equals,则应该重写 hashcode() 以维护 hashcode() 的一般契约 - 相等的对象必须具有相同的 hashcode(反之则不然,因为两个对象可以具有相同的 hashcode)散列但不相等。)
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
The above statement is redundant.
More specifically, it's simply checking to see if you are comparing an object to itself ... so it can skip the code below it. For example:
Note: As an aside, if one overrides equals, one should override hashcode() to maintain the general contract for hashcode() - equal objects must have the same hashcode (the reverse is not true since two objects could have the same hash but not be equal.)
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
在 Eclipse 中,您可以选择“生成 hashCode() 和 equals()...”(菜单源)
In eclipse you have the option to "Generate hashCode() and equals()..." (menu Source)