下面给出编写一个完美的equals方法的建议:
1.显式参数命名为otherObject,稍后需要将它强制转换成另一个名为other的变量。
2.检测this与otherObject是否相等:
if(this == otherObject) return true;
这条语句只是一个优化,实际上,这是一种经常采用的形式。因为检测身份要比逐个检测字段的开销要小的多。
3.检测otherObject是否为null,如果为null,直接返回false,这项检测很有必要。
if(otherObject == null) return false;
4.比较this与otherObject的类,如果equals的语义可以在子类中改变,就使用getClass检测:
if(getClass() != otherObject.getClass()) return false;
如果所有的之子类都有相同的相等性语义,可以使用instanceof检测:
if(!(otherObject instanceof ClassName)) return false;
5.将otherObject 强制类型转换为相应类型的变量
ClassName other = (ClassName) otherObject;
6.现在根据相等性原则比较字段,使用 == 比较基本类型字段,使用 Objects.equals 比较对象字段。如果所有的字段都匹配,就返回true,否则返回false。
return field1 == other.field1
&& Objects.equals(filed2,other.filed2)
&& ...;
如果在子类中重新定义equals方法,就要在其中包含一个super.equals(other)调用