【发布时间】:2010-07-17 16:31:33
【问题描述】:
我正在尝试更好地理解equals() 方法。我见过的所有例子都是这样的:
public class City
{
public boolean equals(Object other)
{
if (other instanceof City && other.getId().equals(this.id))
{
return true;
}
// ...
}
}
该方法必须采用 Object 而不是 City 吗?
例如这是不允许的吗?
public class City
{
public boolean equals(City other)
{
if (other == null)
{
return false;
}
return this.id.equals(other.getId());
}
}
【问题讨论】:
标签: java equals referenceequals