重写equals方法

  用下面的例子来进行解释。

  

    String name;
    int id;

    @Override
    public boolean equals(Object otherObject) {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (this.getClass() != otherObject.getClass())
            return false;
        if(!(otherObject instanceof father))//father 代表它们的父类,如果子类都具有同样的语义可以使用这种方法。
            return false;
        Test test = (Test) otherObject;
        return this.name.equals(test.name) && this.id == test.id;
    }

  

原文地址:https://www.cnblogs.com/benniaoxuefei/p/5631434.html