由Eclipse生成的equals():getOuterType()? [英] equals() generated by Eclipse: getOuterType()?
问题描述
我有一个简单的类Point,两个字段类型为 double
。我要求Eclipse 3.6为它生成 equals()
和 hashCode()
。 equals()
方法如下所示:
I have simple class Point with two fields of type double
. I asked Eclipse 3.6 to generate equals()
and hashCode()
for it. The equals()
method looks like this:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
return true;
}
而 getOuterType
看起来像这样:
private Point getOuterType() {
return Point.this;
}
所以问题是: getOuterType的目的是什么().equals(other.getOuterType())
line?
So the question is: what's the purpose of getOuterType().equals(other.getOuterType())
line?
推荐答案
是一个内部类(非静态嵌套类),它有一个外部封闭的实例。内部类型的两个对象也不是真的相等,除非包围的实例是相等的;外部实例就像一个隐藏的字段(通常命名为这个$ 0
由 javac
)。
Well, if your class is an inner class (non-static nested class), it has an outer, enclosing instance. Two objects of an inner class type aren't really equal unless the enclosing instances are equal, too; the outer instance like a hidden field (usually named this$0
by javac
).
这篇关于由Eclipse生成的equals():getOuterType()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!