我有一个简单的类 Point,它有两个 double
类型的字段。我要求 Eclipse 3.6 为其生成 equals()
和 hashCode()
。 equals()
方法如下所示:
@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())
行的目的是什么?
最佳答案
嗯,如果你的类是一个内部类(非静态嵌套类),它有一个外部的封闭实例。内部类类型的两个对象并不真正相等,除非封闭实例也相等;外部实例就像一个隐藏字段(通常由 javac
命名为 this$0
)。
关于java - Eclipse : getOuterType()? 生成的 equals(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850633/