這是個好主意嗎?現在如何解決,如果沒有的話?實現對象的「絕對等於」方法(稱爲same()或same())。好主意?
我認爲這將是有趣添加
final boolean identical(Obj obj){
return (this==obj);
}
,所以我們有一個改進的equals(邏輯等於)
boolean equals (Obj obj){
return identical(obj); // by default, but its overrideable
}
這個問題從需求arised在這個其他問題(A Mechanism for having different equals (physical equals and logical equals) on objects in Collection)有一種方法可以將相同指針的列表與同等對象列表進行比較。有了這個想法,我們可以添加到收集界面:
coll.equals(coll2)
coll.identical(coll2)
coll.identicalElem(coll2){
//current equals implementation of collections but calling identical to compare objects
}
您怎麼看?
你的方法和'=='有什麼區別? – 2012-08-01 11:50:51
如果你想比較身份而不是「平等」,只是不要在你自己的對象中重寫'equals()'... – 2012-08-01 11:52:03
這是一個重新發明車輪的例子。主要的(在我看來)這發生在有人發現自己比他們面前的所有計算機科學家都更聰明。 – Woot4Moo 2012-08-01 12:25:03