阅读背景:

重写equals

来源:互联网 
public boolean equals(Object otherObject) {
    if (this == otherObject) return true;
    if (otherObjuct == null) return false;
    //如果equals的语义在每个子类中有所改变,就使用getClass检测
    if (getClass()!= otherObject.getClass()) return false;
    //如果所有的子类都拥有统一的语义,就使用instanceof检测
    if (!(otherObject instanceof ClassName)) return false;
    ClassName other = (ClassName)otherObject;
    //使用==比较基本数据类型,使用equals比较对象
    return field1 == other.field1 && Objects.equals(field2, other.field2) && ...;
}public boolean equals(Object otherObject)



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: