我有简单的类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;
}
Run Code Online (Sandbox Code Playgroud)
而getOuterType看起来像这样:
private Point getOuterType() {
return Point.this;
}
Run Code Online (Sandbox Code Playgroud)
所以问题是:getOuterType().equals(other.getOuterType())线的目的是什么?
Chr*_*ung 13
好吧,如果你的类是一个内部类(非静态嵌套类),它有一个外部的封闭实例.除非封闭实例相等,否则内部类类型的两个对象实际上并不相等; 外实例等的隐藏字段(通常命名为this$0通过javac).
| 归档时间: |
|
| 查看次数: |
2312 次 |
| 最近记录: |