Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Objects;
- import java.time.LocalDate;
- public class Human {
- String name;
- String surName;
- int age;
- LocalDate dateOfBirth;
- int iin;
- public Human(String name, String surName, int age, LocalDate dateOfBirth, int iin) {
- this.name = name;
- this.surName = surName;
- this.age = age;
- this.dateOfBirth = dateOfBirth;
- this.iin = iin;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Human human = (Human) o;
- return iin == human.iin;
- }
- @Override
- public int hashCode() {
- return Objects.hash(iin); // хэш тоже строим по IIN
- }
- }
- public class Main {
- public static void main(String[] args) {
- Human h1 = new Human("Yerkebulan", "Orda", 30, LocalDate.of(1995, 5, 5), 123456789);
- Human h2 = new Human("Yerkebulan", "Orda", 30, LocalDate.of(1995, 5, 5), 123456789);
- Human h3 = new Human("Almas", "Omarov", 22, LocalDate.of(2003, 3, 3), 987654321);
- System.out.println("h1.equals(h2): " + h1.equals(h2));
- System.out.println("h1.equals(h3): " + h1.equals(h3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment