如何在 Java 中重写 equals 方法

2022-08-31 08:34:09

我试图在Java中覆盖相等的方法。我有一个类,基本上有2个数据字段和.现在我想重写方法,以便我可以在2 People对象之间进行检查。Peoplenameageequals

我的代码如下

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)

溶液

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


答案 1
//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 false

-- Subash Adhikari - VS - StackOverflow false

-- Subash Adhikari - VS - Subash Adhikari true

-- K - VS - 堆栈溢出错误

-- K - VS - Subash Adhikari false

-- StackOverflow - VS - Subash Adhikari false

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


答案 2

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

public boolean equals(People other){

此处与 不同。PeopleObject

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

@Override
public boolean equals(Object other){

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


推荐