首先是超类Employee:
package chapter5_inheritance.equals;import java.time.LocalDate;
import java.util.Objects;public class Employee {private String name;private double salary;private LocalDate hireDay;public Employee(String name, double salary, int year, int month, int day) {this.hireDay = LocalDate.of(year, month, day);this.name = name;this.salary = salary;}public String getName() {return name;}public double getSalary() {return salary;}public LocalDate getHireDay() {return hireDay;}public void raiseSalary(double byPercent) {double delta = salary * byPercent / 100;salary += delta;}public boolean equals(Object otherObject) {if (this == otherObject) {return true;}if (otherObject == null) {return false;}if (getClass() != otherObject.getClass()) {return false;}Employee other = (Employee) otherObject;return Objects.equals(name, other.name) && salary == other.salary&& Objects.equals(hireDay, other.hireDay);}public int hashCode() {return Objects.hash(name, salary, hireDay);}public String toString() {return getClass().getName() + "[name=" + name + ",salary=" + salary+ ",hireDay=" + hireDay + "]";}
}
其次是子类Manager:
package chapter5_inheritance.equals;public class Manager extends Employee{private double bonus;public Manager(String name, double salary, int year, int month, int day) {super(name, salary, year, month, day);bonus = 0;}public void setBonus(double bou) {bonus = bou;}public double getSalary() {double baseSalary = super.getSalary();return baseSalary + bonus;}public boolean equals(Manager otherObject) {if(!super.equals(otherObject)) return false;return bonus == ((Manager) otherObject).bonus;}public int hashCode() {return java.util.Objects.hash(super.hashCode(), bonus);}public String toString() {return super.toString() + "[bonus=" + bonus + ']';}
}
最后主函数代码:
package chapter5_inheritance.equals;import java.util.Objects;public class equals_test {public static void main(String[] args){var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);var alice2 = alice1;var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);System.out.println("alice1 == alice2: " + (alice1 == alice2)); /* 引用都是一样的*//* true */System.out.println("alice1 == alice3: " + (alice1 == alice3)); /* false */System.out.println("----------Have a test-----------------");System.out.println(alice1 == alice3); /* false */System.out.println(alice1.getClass() == alice3.getClass()); /* true */System.out.println(Objects.equals(alice1.getName(), alice3.getName())); /* true */System.out.println(alice1.getSalary() == alice3.getSalary()); /* true */System.out.println(Objects.equals(alice1.getHireDay(), alice3.getHireDay())); /* true */System.out.println("---------------------------------------");System.out.println("alice1.equals(alice3): " + alice1.equals(alice3)); /* true */System.out.println("alice1.equals(bob): " + alice1.equals(bob)); /* false */System.out.println("bob.toString(): " + bob);System.out.println();var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);boss.setBonus(5000);System.out.println("boss.toString(): " + boss);System.out.println("carl.equals(boss): " + carl.equals(boss)); /* false */System.out.println("alice1.hashCode(): " + alice1.hashCode());System.out.println("alice3.hashCode(): " + alice3.hashCode()); /* 这两个散列码是一样的 */System.out.println("bob.hashCode(): " + bob.hashCode());System.out.println("carl.hashCode(): " + carl.hashCode());/*alice1 == alice2: truealice1 == alice3: falsealice1.equals(alice3): truealice1.equals(bob): falsebob.toString(): chapter5_inheritance.equals.Employee[name=Bob Brandson,salary=50000.0,hireDay=1989-10-01]boss.toString(): chapter5_inheritance.equals.Manager[name=Carl Cracker,salary=80000.0,hireDay=1987-12-15][bonus=5000.0]carl.equals(boss): falsealice1.hashCode(): -808853550alice3.hashCode(): -808853550bob.hashCode(): -624019882carl.hashCode(): -2004699436**/}
}