标签:
我们都知道,==是用来比较引用的(物理上的相等),而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 }
破坏对称性
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 }
破坏传递性
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 }
在编写equals代码时,一些好的习惯是非常必要的
标签:
原文地址:http://www.cnblogs.com/cookiezhi/p/5743778.html