进阶day03-对象的哈希值(hashCode())
This commit is contained in:
29
javaSE-day03/src/com/inmind/set02/Student.java
Normal file
29
javaSE-day03/src/com/inmind/set02/Student.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user