欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
java equals使用instanceof還是getClass?

'm using Eclipse to generate .equals() and .hashCode(), and there is an option labeled "Use 'instanceof' to compare types". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer .getClass() over instanceof?

Without using instanceof:

if (obj == null)  return false;if (getClass() != obj.getClass())  return false;

Using instanceof:

if (obj == null)  return false;if (!(obj instanceof MyClass))  return false;

I usually check the instanceof option, and then go in and remove the "if (obj == null)" check. (It is redundant since null objects will always fail instanceof.) Is there any reason that's a bad idea?

share|improve this question

10 Answers

up vote 44 down vote accepted

If you use instanceof, making your equals implementation final will preserve the symmetry contract of the method: x.equals(y) == y.equals(x). If final seems restrictive, carefully examine your notion of object equivalence to make sure that your overriding implementations fully maintain the contract established by the Object class.

share|improve this answer
    
exactly that came to my mind when i read the josh bloch quote above. +1 :) –  Johannes Schaub - litb Feb 27 '09 at 21:43
3  
definitely the key decision. symmetry must apply, and instanceof makes it very easy to be asymmetric accidentally –  Scott Stanchfield Mar 3 '09 at 20:15

Josh Bloch favors your approach:

The reason that I favor the instanceof approach is that when you use the getClass approach, you have the restriction that objects are only equal to other objects of the same class, the same run time type. If you extend a class and add a couple of innocuous methods to it, then check to see whether some object of the subclass is equal to an object of the super class, even if the objects are equal in all important aspects, you will get the surprising answer that they aren't equal. In fact, this violates a strict interpretation of the Liskov substitution principle, and can lead to very surprising behavior. In Java, it's particularly important because most of the collections (HashTable, etc.) are based on the equals method. If you put a member of the super class in a hash table as the key and then look it up using a subclass instance, you won't find it, because they are not equal.

See also this SO answer.

Effective Java chapter 3 also covers this.

share|improve this answer
2  
+1 Heh as soon as I saw this question I thought of the Josh Bloch quote. –  cletus Feb 27 '09 at 22:11
    
@cletus: I couldn't believe that no one had posted it before I did. –  Michael Myers? Feb 27 '09 at 22:12
3  
+1 Everyone doing java should read that book at least 10 times! –  André Feb 27 '09 at 23:32
1  
The problem with the instanceof approach is that it breaks the "symmetric" property of Object.equals() in that it becomes possible that x.equals(y) == true but y.equals(x) == false if x.getClass() != y.getClass(). I prefer not to break this property unless absolutely necessary (i.e., overriding equals() for managed or proxy objects). –  Kevin Sitze Jun 25 '13 at 23:13
1  
The instanceof approach is proper when, and only when, the base class defines what equality among subclass objects should mean. Using getClass does not violate the LSP, since the LSP merely relates to what can be done with existing instances--not what kinds of instances can be constructed. The class returned by getClass is an immutable property of an object instance. The LSP does not imply that it should be possible to create a subclass where that property indicates any class other than the one which created it. –  supercat Jan 30 at 0:14
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java – 基類(lèi)和子類(lèi)中的equals方法
對象相等比較:深入equals方法
透徹理解 KVO 觀(guān)察者模式(附基于runtime實(shí)現代碼)
java 反射輸出對象的所有屬性
Java英文面試題(核心知識篇)
Method類(lèi)invoke方法的使用
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久