package com.inmind.set02; import java.util.Objects; public class Student { String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } //如果没有重写equals方法,默认使用==比较地址来判断是否相同 //已经重写了equals方法,比较属性的内容 @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && name.equals(student.name); } //当前学生类的哈希值,跟内容有关,属性相同的学生对象的哈希值必定相同 @Override public int hashCode() { int result = name.hashCode(); result = 31 * result + age; return result; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }