设计原则
自反性x.equals(x)返回true
对称性y.equals(x)和x.equals(y)返回相同
传递性x.equals(y)返回true,y.equals(z)返回true,x.equals(z)也返回true
一致性x,y对象引用没有发生变化,反复调用x.equals(y)结果相同
建议
显示参数命名为otherObject
检测this与otherObject是否引用同一个对象if(this==otherObject) return true;
为空对象? if(otherObject==null) return false;
同一个类? if(getClass() != otherObject.gerClass()) return false;
是否与继承关系? if(!(otherObject instanceof ClassName)) return false;
比较域ClassName other =(ClassName)otherObject;return field1==other.field1;