這篇總結(jié)的形式是提出個問題,然后給出問題的答案。這是目前學習知識的一種嘗試,可以讓學習更有目的。
Q1.什么時候應當重寫對象的equals方法?
答:一般在我們需要進行值比較的時候,是需要重寫對象的equals方法的。而例外情況在《effective java》的第7條“在改寫equals的時候請遵守通用約定”中清楚描述了。
我們知道,在Java中,每個對象都繼承于Object.如果不重寫,則默認的equals代碼如下所示:
1
2
3
|
public boolean euqals(Object obj){ return this == obj; } |
由上面的代碼可以看出,equal默認是使用“==”來判斷兩個對象是否相等。兩個對象使用“==”比較的是對象的地址,只有兩個引用指向的對象相同的時候,“==”才返回true。所以,在開頭的例子中,就需要重寫equals方法,讓兩個對象有equals的時候。
Q2.如何重寫equals?
答:首先,當改寫equals方法時,需要保證滿足它的通用約定。這些約定如下所示:
自反性,對于任意的引用值x,x.equals(x)一定為true。
對稱性,對于任意的引用值x和y,當且僅當y.equals(x)時,x.equals(y)也一定返回true.
傳遞性,對于任意的引用值x,y,z。如果x.equals(y)返回true,y.euqals(z)返回true,則x.equals(z)也返回true。
一致性,對于任意的引用值x和y,如果用于equals比較的對象信息沒有修改,那么,多次調(diào)用x.equals(y)要么一致返回true,要么一致返回false.
非空性,所有的對象都必須不等于null。
其實我覺的一個簡單的方法是參照String的equals方法即可,官方出版,滿足各種要求。其代碼如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public boolean equals(Object anObject) { if ( this == anObject) { return true ; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n– != 0 ) { if (v1[i++] != v2[j++]) return false ; } return true ; } } return false ; } |
函數(shù)的解釋如下所示:
- 使用==檢查“實參是否是指向?qū)ο蟮囊粋€引用”。
- 使用instanceof檢查實參是否和本對象同類,如果不同類,就不相等。
- 將實參轉(zhuǎn)換為正確的類型。
- 根據(jù)類的定義,檢查實現(xiàn)此對象值相等的各個條件。
更詳細的信息,還是請看《effective java》的第7條“在改寫equals的時候請遵守通用約定”。
Q3.修改equals時需要注意什么?
答:大致需要注意以下幾點:
若修改equals方法,也請修改hashCode方法
首先這個是語言的一個約定,這么做的一個原因是當此對象作為哈希容器的元素時,需要依賴hashCode,對象默認的hashCode是返回一個此對象特有的hashCode,不同的對象的hashCode返回值是不一樣的,而哈希容器處理元素時,是按照對象的哈希值將對象分配到不同的桶中,若我們不重寫對象的hashCode,那么值相等的對象產(chǎn)生的哈希值也會不同,這樣當在哈希容器中查找時,會找不到對應的元素。
更詳細的信息請看《effective Java》的第8條“改寫equals時總是要改寫hashCode”。
重寫時保證函數(shù)聲明的正確
請注意equals的聲明是
1
|
public boolean equals(Object obj) |
參數(shù)類型是Object,如果參數(shù)類型是此對象類型的話,如下:
1
2
3
4
5
6
7
8
9
10
11
|
class Point{ final int x; final int y; public void Point( int x, int y) this .x = x; this .y = y; } public boolean euqals(Point obj){ return ( this .x == obj.x && this .y == obj.y); } } |
下面代碼執(zhí)行是按照我們的預期執(zhí)行的。
1
2
3
|
Point a( 1 , 2 ); Poinr b( 1 , 2 ); System.out.println(a.equals(b)); // 輸出true |
但是如果將類A放入容器中,則會出問題
1
2
3
4
5
|
import java.util.HashSet; HashSet<Point> coll = new HashSet<Point>(); coll.add(a); System.out.println(coll.contains(b)); // 輸出false |
這是由于HashSet中的contains方法中調(diào)用的是equals(Object obj),而Point中的equals(Object obj)仍是Object的equals,這個方法在前面已經(jīng)說過了,比較的是對象的地址,所以在coll中調(diào)用contains(b)時,當然得不到true。
當有繼承關(guān)系時注意equals的正確
當一個類重寫equals方法后,另一個類繼承此類,此時,可能會違反前面說到的對稱性,代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class ColoredPoint extends Point { private final Color color; public ColoredPoint( int x, int y, Color color) { super (x, y); this .color = color; } @Override public boolean equals(Object other) { boolean result = false ; if (other instanceof ColoredPoint) { ColoredPoint that = (ColoredPoint) other; result = ( this .color.equals(that.color) && super .equals(that)); } return result; } } |
當我們作比較時
1
2
3
4
|
Point p = new Point( 1 , 2 ); ColoredPoint cp = new ColoredPoint( 1 , 2 , Color.RED); System.out.println(p.equals(cp)); //輸出ture System.out.println(cp.equals(p)); //輸出false |
原因是當調(diào)用Point.equals的時候,只比較了Point的x和y坐標,同時ColoredPoint也是Point類型,所以上面第三行代碼相等,而調(diào)用ColoredPoint的時候,Point不是ColoredPoint類型,這樣就導致第四行代碼輸出false。
若我們忽略Color的信息來比較呢,例如將ColoredPoint的equals方法改為:
1
2
3
4
5
6
7
8
9
10
11
12
|
@overwrite public boolean equals(Object obj){ if ((obj instanceof Point)){ return false ; } if (!(obj instanceof ColoredPoint)){ return obj.equals( this ); } return super .equals(obj) && ((ColoredPoint)obj).color == color; } |
這樣就保證了對稱性,但是卻違反了傳遞性,即下面的情況:
1
2
3
4
5
6
|
ColoredPoint cp1 = new ColoredPoint( 1 , 2 , Color.RED); Point p = new Point( 1 , 2 ); ColoredPoint cp2 = new ColoredPoint( 1 , 2 , Color.BLUE); System.out.println(cp1.equals(p)); //true System.out.println(p.equals(cp2)); //true System.out.println(cp1.equals(cp2)); //false |
面對這種情況,大致有兩種解決方案,一種酷殼的文章–如何在Java中避免equals方法的隱藏陷阱的最后一條,斷絕了Point和ColoredPoint相等的可能,這是一種處理方法,認為Point和ColoredPoint是不同的。另一種方法是effective Java上提出的,使用聚合而不是繼承,將Point作為ColoredPoint的一個成員變量。目前我傾向于這種方法,因為聚合比繼承更靈活,耦合更低。這種方法的代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class ColoredPoint{ private final Point point; private final Color color; public Point asPoint(){ return point; } public boolean equals(Object obj){ boolean ret = false ; if (obj instanceof ColoredPoint){ ColoredPoint that = (ColoredPoint)obj; ret = that.point.equals(point) && color.equals(that.color); } return ret; } } |
當ColoredPoint需要比較坐標時,可以調(diào)用asPoint方法來轉(zhuǎn)化為坐標進行比較。其他情況比較坐標和顏色,這樣就可以解決上面關(guān)于對稱性和傳遞性的問題了。
以上就是全文的內(nèi)容,由于水平有限,文章中難免會有錯誤,希望大家指正,謝謝。
希望本文對大家的學習有所幫助,也希望大家多多支持服務器之家。