package fivetwo; public class ObjectExtend extends Object{ public static void main(String[] args) { } public static boolean equals(Object a,Object b){ return a==b || (a!=null && a.equals(b)); } } package fivetwo; public class Employee{ private String str; private int x; public static void main(String[] args) { } public static void method1(){ } 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 (this.x == other.x && this.str.equals(other.str)); return (this.x == other.x && ObjectExtend.equals(this.str, other.str)); } //Objects.equals jdk的高版本非空判断 } package fivetwo; public class Manager extends Employee{ private int bonus; public static void main(String[] args) { // TODO Auto-generated method stub Manager m1 = new Manager(); Manager m2 = new Manager(); m1.equals(m2); } //子类的比较先调用超类的equal方法,超类的域不相等就不可能相等 public boolean equals(Object otherObject){ if (!super.equals(otherObject)) { return false; } Manager other = (Manager)otherObject; return this.bonus == other.bonus; } }