比较Java中两个对象的内容

问题描述

我试图在同一类的两个对象之间进行比较。实际上,我想比较两个对象的内容在这里,对象属于 Student 类。在 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);
        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 类有问题。但问题是我不太确定。另外,我不明白如何解决这个问题。有人可以告诉我这里出了什么问题。 提前致谢。

解决方法

您可以像 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。但是,由于这看起来是一个学校项目,我想您可能需要手动检查或假设值不会为空,而不是使用标准库。

,

要使 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",10))));
        System.out.println(new Student("John",new Date(11,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);
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...