java 如何完美编写对象的equal方法

定义比较的对象叫otherObject

步骤如下:

1.检测this与otherObject是否为相同的引用

if(this==otherObject) return true;


2.检测otherObject是否为null

if(otherObject==null) return false;


3.类型比较

(1)如果较真到必须为同一个类,则使用getClass()比较

(2)如果所有子类统一一个标准,则只需要基类相同即可 使用 instanceof


4.类型转换,域值比较

你可能感兴趣的:(java 如何完美编写对象的equal方法)