比较 Java 中两个对象的内容

Comparing the content of two objects in Java

我试图对相同 class 的两个对象进行比较。实际上,我想比较两个对象的内容。在这里,对象是 class 学生。在 class Student 中,我重写了 equals() 方法,如下所示。 这样做,我的意图是否实现(比较两个学生的名字和生日)?如果不是这里发生了什么? 问题是我没有得到我期望的答案。输出是 false,即使它必须是 true.

public class Main {

    public static void main(String[] args) {
        Student a = new Student("John", "Johnny");
        Student b = new Student("John", "Johnny");
        a.setBirthDate(10, 10, 10);
        b.setBirthDate(10, 10, 10);
        boolean ans = Student.equals(a, b);
        System.out.println(ans);
    }

}
public class Date {
    public int day;
    public int month;
    public int year;
    
    public Date(int d, int m, int y) {
        this.day = d;
        this.month = m;
        this.year = y;
    }
}
public class Student{
    private String firstName;
    private String lastName;
    private Date birthDate;
    
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    public void setBirthDate(int day, int month, int year) {
        Date b_day = new Date(day, month, year);
        birthDate = b_day;
    }
}


@Override
    public boolean equals(Object student) {
        System.out.println(this.firstName);
        System.out.println(this.lastName);
        System.out.println(this.birthDate);
        
        System.out.println(((Student)student).firstName);
        System.out.println(((Student)student).lastName);
        System.out.println(((Student)student).birthDate);
        return super.equals(student);
        
    }

我已经重写了 equals 方法,如下所示。但我仍然面临同样的问题。我怀疑 Date class 有问题。但问题是我不太确定。另外,我不明白如何解决这个问题。有人可以告诉我这里出了什么问题吗? 提前致谢。

要使 equals 正常工作,您还需要覆盖 hashCode。进一步的相等性检查需要对您的对象进行实际比较。

此外,任何相关对象也必须实现 hashCodeequals 方法。在这种情况下 Date.

可能的代码

import java.util.Objects;

class Date {

    public int day;
    public int month;
    public int year;

    public Date(int d, int m, int y) {
        this.day = d;
        this.month = m;
        this.year = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Date date = (Date) o;
        return day == date.day &&
            month == date.month &&
            year == date.year;
    }

    @Override
    public int hashCode() {
        return Objects.hash(day, month, year);
    }
}

public class Student {

    private final String firstName;
    private final String lastName;
    private Date birthDate;

    public Student(String firstName, String lastName, Date birthDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
    }

    public static void main(String[] args) {
        System.out.println(new Student("John", "Johnny", new Date(10, 10, 10))
            .equals(new Student("John", "Johnny", new Date(10, 10, 10))));
        System.out.println(new Student("John", "Johnny", new Date(11, 10, 10))
            .equals(new Student("John", "Johnny", new Date(10, 10, 10))));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;
        return Objects.equals(firstName, student.firstName) &&
            Objects.equals(lastName, student.lastName) &&
            Objects.equals(birthDate, student.birthDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(firstName, lastName, birthDate);
    }
}

您可以像 if 语句一样构造 equals 方法。您只需要先转换另一个参数,然后才能比较它们的字段。

@Override
public boolean equals(Object o) {
    // Check if the other object is also a Student
    if (o instanceof Student) {
        // Now that we know o is a student, we can safely cast it
        Student other = (Student) o;

        // Similarly to how you would write an if statement you can compare each individual field.
        // Thanks to inheritance, we defer the equality check of each field to its own implementation
        return this.firstName.equals(other.firstName)
            && this.lastName.equals(other.lastName)
            && this.birthDate.equals(other.birthDate);
    }

    // Other object was not a student
    return false;
}

然后你需要去Date写类似的东西,这样当你比较birthDate时,它就会知道该怎么做。

您还可以更进一步,使用 Objects.equals(a, b) 而不是 a.equals(b)。如果 a 在比较它们时恰好为 null,这将确保您不会得到 nullPointerException。但是,由于这看起来是一个学校项目,我想您可能需要手动检查或假设值不会为空,而不是使用标准库。