是否有任何设计模式(或成语)有助于equals()
在 Java 中实现方法?
这项任务并不难,但在大多数情况下,它几乎是一样的......这就是为什么我猜有一个模式,但我没有找到它。
UPDATE
我选择了这个方法:generate equals() method in Eclipse
但是......我找到了一个好方法(在 AbstractList 中)来使生成的代码更好:
if (!(attributes ==null ? other.attributes==null : attributes.equals(other.attributes)))
return false;
而不是生成:
if (attributes == null) {
if (other.attributes != null)
return false;
} else if (!attributes.equals(other.attributes))
return false;