覆盖equals时遵守通用约定
1. 尽量避免覆盖equals方法:
因为覆盖equals方法看似很简单,但实际上有许多覆盖方式会导致错误,并且后果很严重。
2. 什么情况下,不需要覆盖equals方法?
- 类的每个实例本质上都是唯一的。
对于代表活动实体而不是值的类来说确实如此,比如Thread类。
- 不关心类是否提供了“逻辑相等”的测试功能。
简单来说,就是我们所设计的类不要去判断实例之间是否相等。
- 超类已经覆盖了equals,从超类继承过来的行为对于子类也是合适的。
比如,我们创建一个Man类,并定义其id(类似身份证)作为判断是否为同一个Man对象。为了满足需求,我们需要创建一个OldMan类继承自Man类,这时候,OldMan中要判断是否为同一对象,完全可以用继承于父类的equals方法。
- 类是私有的或是包是私有的,可以确定它的equals方法永远不会被调用。
在这种情况下,是需要覆盖equals方法,以防止它被意外调用:
@Override
public boolean equals(Object o){
throw new AssertionError();
}
3. 什么情况下需要覆盖equals方法?
如果类具有自己特定的“逻辑相等”(不同于对象等同的概念),而且超类还没有覆盖equals以实现自己期望的行为,这时候我们就需要覆盖equals方法。
比如,我们创建的Man类,没有定义equals方法,当我们再创建其子类OldMan时,如果这时候需要判断两个OldMan对象是否相同,那么这时候OldMan就需要覆盖equals方法。
4. 覆盖equals方法所要遵守的通用约定:
- 自反性:对于任何非null的引用值x, x.equals(x)必须返回true。
- 对称性:对于任何非null的引用值x和y,当且仅当y.equals(x)返回true时,x.equals(y)必须返回true。
- 传递性:对于任何非null的引用值,如果x.equals(y)返回true,并且y.equals(z)返回true,那么x.equals(y)也必须返回true。
- 一致性:对于任何非null的引用值x和y,只要equals的比较操作在对象中所用的信息没有被修改,多次调用x.equals(y)就会一致地返回true,或者一致地返回false。
- 非空性:对于任何非null的引用值x,x.equals(null)必须返回false。
-
自反性:这个应该很好理解,就是要满足对象必须等于其本身,一般来说,是很少违反的。
-
对称性:我们看下一个关于字符串的例子
public class CaseInsensitiveString {
private final String s;
public CaseInsensitiveString(String s){
if (s == null)
throw new NullPointerException();
this.s = s;
}
@Override
public boolean equals(Object o){
if (o instanceof CaseInsensitiveString)
return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
if (o instanceof String)
return s.equalsIgnoreCase((String) o);
return false;
}
public static void main(String[] args){
CaseInsensitiveString cis = new CaseInsensitiveString("Test");
String s = "test";
System.out.println("cis.equals(s) : " + cis.equals(s));
System.out.println("s.equals(cis) : " + s.equals(cis));
}
}
输出结果:
cis.equals(s) : true
s.equals(cis) : false
这显然违背了equals的对称性原则,我们来解析下覆盖的equals方法:
@Override
public boolean equals(Object o){
//如果传入的对象为CaseInsensitiveString类型,则通过比较该类型的成员变量s来判断对象是否相同
if (o instanceof CaseInsensitiveString)
return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
//如果传入的对象是String类型,则类中的成员变量与s直接比较
if (o instanceof String)
return s.equalsIgnoreCase((String) o);
return false;
}
实际上,cis.equals(s)原本应该为false,因为cis和s两者本身就不是同一种类型,那么为什么会导致其返回的是true呢?原因就在于:使用cis对象中的成员变量s与字符串进行比较
if (o instanceof String)
return s.equalsIgnoreCase((String) o);
这种情况,只能说cis中的成员变量s和字符串s是相同的,但不能说cis对象和字符串s对象是相同的。
因此,要解决这个问题,实际上,很简单,只要将后面那段删除即可:
@Override
public boolean equals(Object o) {
return o instanceof CaseInsensitiveString && s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
}
输出结果:
cis.equals(s) : false
s.equals(cis) : false
- 传递性:
我们先创建一个Point类:
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;
}
}
可以看到,我们以Point中x和y都相同才算为同一个对象。
接下来,我们需要扩展下这个类,为其添加颜色信息:
public class ColorPoint extends Point {
enum Color{
RED,BLACK,WHITE
}
private final Color color;
public ColorPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ColorPoint))
return false;
return super.equals(o) && ((ColorPoint) o).color == color;
}
}
这时候,我们重写了equals方法,因为如果继续使用父类的equals方法,会忽略掉颜色信息。
好,我们来看下这样重写的equals方法是否正确:
public static void main(String[] args){
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2 , Color.BLACK);
System.out.println("p.equals(cp): " + p.equals(cp));
System.out.println("cp.equals(p): " + cp.equals(p));
}
输出结果:
p.equals(cp): true
cp.equals(p): false
我们可以看到这明显违反了"自反性“原则,为什么会产生这样的结果呢?
实际上,p.equals(cp)调用的是父类的方法,比较的是点的x和y,两者x和y都相同,自然返回true,而cp.equals(p)调用的是子类的equals方法,由于传入的p并不是CorlorPoint类型,因此直接返回false。
那么如何让p和cp比较时可以忽略”颜色信息“,但又不会违反”自反性“呢?
@Override
public boolean equals(Object o) {
//不属于Point类和ColorPoint类的情况
if(!(o instanceof Point))
return false;
//属于Point类的情况
if (!(o instanceof ColorPoint))
return o.equals(this);
//属于ColorPoint类的情况
return super.equals(o) && ((ColorPoint) o).color == color;
}
public static void main(String[] args){
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2 , Color.BLACK);
System.out.println("p.equals(cp): " + p.equals(cp));
System.out.println("cp.equals(p): " + cp.equals(p));
System.out.println("cp instanceof Point: " + (cp instanceof Point));
}
输出结果:
p.equals(cp): true
cp.equals(p): true
cp instanceof Point: true
我们可以看到”自反性“的问题已经解决了,这里要注意的一点是: 子类instanceof父类 一定是true的。
那么,这样的修改是否就完美了呢?我们再进行一个小测试:
public static void main(String[] args){
ColorPoint p1 = new ColorPoint(1, 2 , Color.BLACK);
Point p2 = new Point(1, 2);
Point p3 = new ColorPoint(1, 2 , Color.WHITE);
System.out.println("p1.equals(p2): " + p1.equals(p2));
System.out.println("p2.equals(p3): " + p2.equals(p3));
System.out.println("p1.equals(p3): " + p1.equals(p3));
}
输出结果:
p1.equals(p2): true
p2.equals(p3): true
p1.equals(p3): false
根据”传递性“,p1.equals(p3)应该为true,而这里为false,明显违反了该原则。实际上,根据ColorPoint中的equals方法,这个结果很明显是false,因为p1和p3同为ColorPoint类型,因此,返回return super.equals(o) && ((ColorPoint) o).color == color;
,两者x和y相同,但color是不同的,因此为false。
那么如何解决”传递性“问题呢?
实际上,这是面向对象语言中关于等价关系的一个基本问题。我们无法在扩展可实例化的类的同时,既增加新的值组件,同时又保留equals约定。
虽然没有一种很好的办法可以既扩展不可实例化的类,又增加值组件,但有一种不错的权宜之计:根据原则”复合优先于继承“,我们不再让ColorPoint扩展Point,而是在ColorPoint中加入一个私有的point域。
public class ColorPoint{
enum Color{
RED,BLACK,WHITE
}
private final Color color;
private final Point point;
public ColorPoint(int x, int y, Color color) {
this.color = color;
point = new Point(x, y);
}
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);
}
public static void main(String[] args){
ColorPoint p1 = new ColorPoint(1, 2 , Color.BLACK);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2 , Color.WHITE);
System.out.println("p1.equals(p2): " + p1.equals(p2));
System.out.println("p2 equals(p1): " + p2.equals(p1));
System.out.println("p2.equals(p3): " + p2.equals(p3));
System.out.println("p1.equals(p3): " + p1.equals(p3));
}
}
输出结果:
p1.equals(p2): false
p2 equals(p1): false
p2.equals(p3): false
p1.equals(p3): false
如此,我们便解决了”自反性“和”传递性“问题了。
-
一致性:如果两个对象相等,它们就必须始终相等,除非它们中有一个对象(或两个都)被修改了。
-
非空性:所有的对象都必须不等于null。因此,我们在覆盖equals方法时,必须先检查其正确类型。
@Override
public boolean equals(Object o){
if (! ( o instanceof MyType))
return false;
MyType mt = (MyType) o;
....
如果漏掉这一步的类型检查,并且传递给equals方法的参数又是错误的类型,那么equals方法将会抛出ClassCastException异常。
覆盖equals方法的建议:
- 使用==操作符检查 ”参数是否为这个对象的引用“。
- 使用instanceof操作符检查 ”参数是否为正确的类型“。
- 把参数转换为正确的类型。
- 对于该类中的”关键域“,检查参数中的域是否与对象中对应的域相匹配。
- 当完成equals方法后,要检查是否满足四个特性,最好测试检查。
- 覆盖equals时总要覆盖hashCode
- 不要企图让equals方法过于智能
- 不要将equals声明中的Object对象替换为其他的类型。