我們都知道,==是用來比較引用的(物理上的相等),而equals方法是用來比較值的(邏輯上的相等),在許多時候需要重寫equals方法來實現我們的需求,比如把對象放到容器中,然後去查找對象。
在重寫equals 方法時要遵循一些契約:
反例:
破壞自反性
1 public class AntiReflexive { 2 3 private String str; 4 public String getStr() { 5 return str; 6 } 7 8 public void setStr(String str) { 9 this.str = str; 10 } 11 12 public boolean equals(Object o) { 13 if (!(o instanceof AntiReflexive)) { 14 return false; 15 } 16 17 AntiReflexive ar = (AntiReflexive)o; 18 return this.str.equals(ar.str.toLowerCase()); // tolowercase 導致了自反性的破壞 19 } 20 21 public static void main(String[] args) { 22 AntiReflexive ar1 = new AntiReflexive(); 23 ar1.setStr("Hello world"); 24 25 System.out.println(ar1.equals(ar1)); 26 } 27 } View Code
破壞對稱性
1 /** 2 * 代碼來自 effective java 3 */ 4 public class AntiSymmetric { 5 6 public static void main(String[] args) { 7 CaseInsensitiveString cis = new CaseInsensitiveString("abc"); 8 String str = "abc"; 9 System.out.println(cis.equals(str)); 10 System.out.println(str.equals(cis)); 11 } 12 } 13 14 class CaseInsensitiveString { 15 16 private String s; 17 18 public CaseInsensitiveString(String s) { 19 this.s = s; 20 } 21 22 public boolean equals(Object o) { 23 if (o instanceof CaseInsensitiveString) { 24 return s.equalsIgnoreCase(((CaseInsensitiveString)o).s); 25 } 26 27 if (o instanceof String) { // 這個地方破壞了對稱性,因為在String中不存在這樣的邏輯,單向的 28 return s.equalsIgnoreCase((String)o); 29 } 30 31 return false; 32 } 33 } View Code
破壞傳遞性
1 public class AntiTransitive { 2 3 public static void main(String[] args) { 4 ColorPoint x = new ColorPoint(10, 20, Color.red); 5 Point y = new Point(10, 20); 6 ColorPoint z = new ColorPoint(10, 20, Color.blue); 7 8 System.out.println("x.equals(y) = " + x.equals(y)); 9 System.out.println("y.equals(z) = " + y.equals(z)); 10 System.out.println("x.equals(z) = " + x.equals(z)); 11 } 12 } 13 14 class Point { 15 private int x; 16 private int y; 17 18 public Point(int x, int y) { 19 this.x = x; 20 this.y = y; 21 } 22 23 public boolean equals(Object o) { 24 if (!(o instanceof Point)) { 25 return false; 26 } 27 28 Point p = (Point)o; 29 30 return x == p.x && y == p.y; 31 } 32 } 33 34 class ColorPoint extends Point { 35 private Color color; 36 37 public ColorPoint(int x, int y, Color color) { 38 super(x, y); 39 this.color = color; 40 } 41 42 public boolean equals(Object o) { 43 if (!(o instanceof Point)) { 44 return false; 45 } 46 47 if (!(o instanceof ColorPoint)) { 48 return o.equals(this); // 這個地方破壞了傳遞性 49 } 50 51 ColorPoint cp = (ColorPoint)o; 52 53 return super.equals(o) && cp.color == this.color; 54 } 55 } View Code
在編寫equals代碼時,一些好的習慣是非常必要的