s-day03--对象的哈希值

This commit is contained in:
xx
2026-10-14 14:00:13 +08:00
parent b5ff8ffb1f
commit d27881af22
2 changed files with 76 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
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;
}
@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;
}
}