以前学习时写的,分享下。。。。
- public boolean equals(Object otherObject) {
- //测试当前对象变量与otherObject对象变量是否引用同一对象
- if (this == otherObject) {
- return true;
- }
- //测试otherObject对象变量是否为空
- if (otherObject == null) {
- return false;
- }
- //测试当前对象类是否和otherObject对象类相同
- if (getClass() != otherObject.getClass()) {
- return false;
- }
- //逐次比较当前类的实例域与otherObject对象的实例域是否相同
- EqualsTest other = (EqualsTest) otherObject;
- return Objects.equals(name, other.name) && id == other.id && salary == other.salary;
- }
复制代码
|
|