在覆盖equals方法的时候,你必须要遵守它的通用约定,不遵守,写出来的方法,会出现逻辑错误。下面是约定的内容:
对称性
public final class CaseInsensitiveString { private final String s ; public CaseInsensitiveString(String s) { if (s == null) throw new NullPointerException(); this. s = s; } // Broken - violates symmetry! @Override public boolean equals(Object o) { if (o instanceof CaseInsensitiveString) return s.equalsIgnoreCase(((CaseInsensitiveString) o). s); if (o instanceof String) // One-way interoperability! return s.equalsIgnoreCase((String) o); return false; } // This version is correct. // @Override public boolean equals(Object o) { // return o instanceof CaseInsensitiveString && // ((CaseInsensitiveString) o).s.equalsIgnoreCase(s); // } public static void main(String[] args) { CaseInsensitiveString cis = new CaseInsensitiveString("Polish" ); String s = "polish"; System.out.println(cis.equals(s) + " " + s.equals(cis)); } }
public class ColorPoint extends Point { private final Color color ; public ColorPoint( int x, int y, Color color) { super(x, y); this.color = color; } // Broken - violates symmetry! @Override public boolean equals(Object o) { if (!(o instanceof ColorPoint)) return false ; return super .equals(o) && ((ColorPoint) o).color == color; } // Broken - violates transitivity! // @Override public boolean equals(Object o) { // if (!(o instanceof Point)) // return false; // // // If o is a normal Point, do a color-blind comparison // if (!(o instanceof ColorPoint)) // return o.equals(this); // // // o is a ColorPoint; do a full comparison // return super.equals(o) && ((ColorPoint)o).color == color; // } public static void main(String[] args) { // First equals function violates symmetry Point p = new Point(1, 2); ColorPoint cp = new ColorPoint(1, 2, Color.RED); System. out.println(p.equals(cp) + " " + cp.equals(p)); // Second equals function violates transitivity ColorPoint p1 = new ColorPoint(1, 2, Color.RED); Point p2 = new Point(1, 2); ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE); System. out.printf("%s %s %s%n" , p1.equals(p2), p2.equals(p3), p1.equals(p3)); } }
public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false ; Point p = (Point) o; return p.x == x && p.y == y; } // See Item 9 @Override public int hashCode() { return 31 * x + y ; } } public enum Color { RED, ORANGE , YELLOW, GREEN, BLUE, INDIGO , VIOLET } public class ColorPoint { private final Point point ; private final Color color ; public ColorPoint(int x, int y, Color color) { if (color == null) throw new NullPointerException(); point = new Point(x, y); this.color = color ; } /** * Returns the point -view of this color point. */ public Point asPoint() { return point ; } @Override public boolean equals(Object o) { if (!(o instanceof ColorPoint)) return false ; ColorPoint cp = (ColorPoint) o; return cp.point .equals(point ) && cp.color.equals( color); } @Override public int hashCode() { return point .hashCode() * 33 + color.hashCode(); } }
* @Override * public boolean equals(Object obj) { * if (!(obj instanceof InetAddress)) { * return false; * } * return Arrays.equals(this.ipaddress, ((InetAddress) obj).ipaddress); * }
原文:http://www.cnblogs.com/ttylinux/p/4363964.html