s-day02-对象的哈希值

This commit is contained in:
2026-08-02 10:49:28 +08:00
parent 70a8eda130
commit b7470d5c07
3 changed files with 55 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
package com.inmind.set02;
import java.util.Objects;
public class Student extends Object {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
int result = Objects.hashCode(name);
result = 31 * result + age;
return result;
}
}