进阶day03-对象的哈希值(hashCode())

This commit is contained in:
2026-02-01 10:34:45 +08:00
parent 0ae9f815f1
commit 87b87d7b5d
2 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
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 && name.equals(student.name);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + age;
return result;
}
}