/*重写equals 重写比较规则,判断两名学员(Student)是否为同一对象 Student相关属性 Id(学号)、name(姓名)、age(年龄) 如果两名学员的学号以及姓名相同,则为同一对象*/ public class Student { private int id; private String name; private int age; @Override public boolean equals(Object obj) { if(obj instanceof Student){ Student s= (Student) obj;//向下转型 return this.id==s.id&&this.name==s.name; } return false; } public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class TestStudent { public static void main(String[] args) { Student s1 = new Student(110,"aa",18); Student s2 = new Student(112,"aa",22); System.out.println(s1.equals(s2)); } }