1、显示参数命名为otherObject, 稍后需要将它转换成另一个other变量/
2、
检测this 与 otherObject是否为同一引用;
if(!(otherObject instanceof ClassName)) return false;
if(this == otherObject) return true;
3、检测ot
h
erObject 是否为null, 如果为null 返回false
if(
otherObject
== null) return false;
4、比较this与otherObject是否属于同一个类。
如果equals 的语义在每个子类中有所变化,就用getClass() 检测:
if(getClass() != otherObject.getClass()) return false;
如果equals 的语义在每个子类都有同样的语义,就用instanceof()检测;
5、将otherObject 转换成相应的类型变量:
ClassName other = (ClassName) otherObject;
6、进行域的比较, == 比较基本类型域, 使用 Objects.equals() 比较对象域
return filed1 == other.filed1 && Objects(filed2, other.filed2) && ...;