Java中如何重写equals方法

新手上路,请多包涵

我正在尝试覆盖 Java 中的 equals 方法。我有一个类 People 它基本上有 2 个数据字段 nameage 。现在我想覆盖 equals 方法,以便我可以检查 2 个 People 对象。

我的代码如下

public boolean equals(People other){
    boolean result;
    if((other == null) || (getClass() != other.getClass())){
        result = false;
    } // end if
    else{
        People otherPeople = (People)other;
        result = name.equals(other.name) &&  age.equals(other.age);
    } // end else

    return result;
} // end equals

但是当我写 age.equals(other.age) 它给了我错误,因为equals方法只能比较字符串并且年龄是整数。

解决方案

我按照建议使用 == 运算符,我的问题就解决了。

原文由 user702026 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 586
2 个回答
//Written by K@stackoverflow
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList<Person> people = new ArrayList<Person>();
        people.add(new Person("Subash Adhikari", 28));
        people.add(new Person("K", 28));
        people.add(new Person("StackOverflow", 4));
        people.add(new Person("Subash Adhikari", 28));

        for (int i = 0; i < people.size() - 1; i++) {
            for (int y = i + 1; y <= people.size() - 1; y++) {
                boolean check = people.get(i).equals(people.get(y));

                System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
                System.out.println(check);
            }
        }
    }
}

//written by K@stackoverflow
public class Person {
    private String name;
    private int age;

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (obj.getClass() != this.getClass()) {
            return false;
        }

        final Person other = (Person) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }

        if (this.age != other.age) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 53 * hash + this.age;
        return hash;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

输出:

跑:

-- Subash Adhikari - VS - K 假

-- Subash Adhikari - VS - StackOverflow 错误

-- Subash Adhikari - VS - Subash Adhikari 真实

-- K - VS - StackOverflow 错误

-- K - VS - Subash Adhikari 错误

-- StackOverflow - VS - Subash Adhikari false

-- 构建成功(总时间:0 秒)

原文由 Kim 发布,翻译遵循 CC BY-SA 4.0 许可协议

引入改变参数类型的新方法签名称为 重载

 public boolean equals(People other){

这里 People 不同于 Object

当方法签名与其超类的方法签名保持相同时,它被称为 重写 并且 @Override 注释有助于在编译时区分两者:

 @Override
public boolean equals(Object other){

没有看到 age 的实际声明,很难说为什么会出现错误。

原文由 fortran 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题