java對(duì)象比較之instanceof與getClass的使用規(guī)則

two distinct scenarios:

? If subclasses can have their own notion of equality, then the symmetry requirement forces you to use the getClass test.

? If the notion of equality is fixed in the superclass, then you can use the instanceof test and allow objects of different subclasses to be equal to one another.

For example:

? In the example with employees and managers, we consider two objects to be equal when they have matching fields. If we have two Manager objects with the same name, salary, and hire date, but with different bonuses, we want them to be different. Therefore, we used the getClass test.

? But suppose we used an employee ID for equality testing. This notion of equality makes sense for all subclasses. Then we could use the instanceof test, and we should have declared Employee.equals as final.


Here is a recipe for writing the perfect equals method:

1. Name the explicit parameter otherObject—later, you will need to cast it to another variable that you should call other.

2. Test whether this happens to be identical to otherObject:

if (this == otherObject) return true;

This statement is just an optimization. In practice, this is a common case. It is much cheaper to check for identity than to compare the fields.

3. Test whether otherObject is null and return false if it is. This test is required.

if (otherObject == null) return false;

4. Compare the classes of this and otherObject. If the semantics of equals can change in subclasses, use the getClass test:

if (getClass() != otherObject.getClass()) return false;

If the same semantics holds for all subclasses, you can use an instanceof test:

if (!(otherObject instanceof ClassName)) return false;

5. Cast otherObject to a variable of your class type:

ClassName other = (ClassName) otherObject

6. Now compare the fields, as required by your notion of equality. Use == for primitive type fields, Objects.equals for object fields. Return true if all fields match, false otherwise.

return field1 == other.field1 && Objects.equals(field2, other.field2) && . . .;

If you redefine equals in a subclass, include a call to super.equals(other).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捌议,一起剝皮案震驚了整個(gè)濱河市掀序,隨后出現(xiàn)的幾起案子砰碴,更是在濱河造成了極大的恐慌,老刑警劉巖善涨,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡遏插,警方通過查閱死者的電腦和手機(jī)验残,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門捞附,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事鸟召〉ò恚” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵药版,是天一觀的道長(zhǎng)辑舷。 經(jīng)常有香客問我,道長(zhǎng)槽片,這世上最難降的妖魔是什么何缓? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮还栓,結(jié)果婚禮上碌廓,老公的妹妹穿的比我還像新娘。我一直安慰自己剩盒,他們只是感情好谷婆,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辽聊,像睡著了一般纪挎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上跟匆,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天异袄,我揣著相機(jī)與錄音,去河邊找鬼玛臂。 笑死烤蜕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的迹冤。 我是一名探鬼主播讽营,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼泡徙!你這毒婦竟也來了橱鹏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤锋勺,失蹤者是張志新(化名)和其女友劉穎蚀瘸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庶橱,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贮勃,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了苏章。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寂嘉。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奏瞬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出泉孩,到底是詐尸還是另有隱情硼端,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布寓搬,位于F島的核電站珍昨,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏句喷。R本人自食惡果不足惜镣典,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望唾琼。 院中可真熱鬧兄春,春花似錦、人聲如沸锡溯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祭饭。三九已至芜茵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間倡蝙,已是汗流浹背夕晓。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留悠咱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓征炼,卻偏偏與公主長(zhǎng)得像析既,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谆奥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容