有什麼設計模式(或習慣用法)有助於在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