@Overridepublic boolean equals(Object otherObject) { //1.检测this 与 otherObject 是否引用同一个对象 if (this == otherObject) return true; //2.检测otherObject 是否为null, 如果为null,返回false if (null == otherObject) return false; //3.比较this 与 ohterObject 是否属于同一个类 if(getClass() != otherObject.getClass()) return false; if (!(otherObject instanceof Employee)) return false; //4. 将otherObject 转换为相应的类类型变量 Employee employee = (Employee) otherObject; //5.域比较 if (id != employee.id) return false; if (Double.compare(employee.salary, salary) != 0) return false; if (name != null ? !name.equals(employee.name) : employee.name != null) return false; return true;} -- 摘《java核心技术:卷一》
时间: 2025-01-01 22:53:23