有什么设计模式(或习惯用法)有助于在Java中实现equals()
方法吗?
这个任务很困难,但在大多数情况下它大致相同...这就是为什么我猜测有一个模式,但我没有找到它。执行equals()方法设计模式
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;
看看这个:http://www.artima.com/lejava/articles/equality.html – flash
我正要发布相同的链接。要特别注意#4,因为它在其他文章中经常被忽略。 – SJuan76