equals()方法的重写

来源:互联网 发布:oracle 数据字典 编辑:程序博客网 时间:2024/05/01 23:34

一、为什么equals()方法要重写?

判断两个对象在逻辑上是否相等,如根据类的成员变量来判断两个类的实例是否相等,而继承Object中的equals方法只能判断两个引用变量是否是同一个对象。这样我们往往需要重写equals()方法。

我们向一个没有重复对象的集合中添加元素时,集合中存放的往往是对象,我们需要先判断集合中是否存在已知对象,这样就必须重写equals方法。

那么,让我们先来看看Object#equals()在Java中的原代码: 

public boolean equals(Object obj) {   return (this == obj); } 

二、怎样重写equals()方法?

  • 重写equals方法的要求:
    1.自反性:对于任何非空引用x,x.equals(x)应该返回true。
    2.对称性:对于任何引用x和y,如果x.equals(y)返回true,那么y.equals(x)也应该返回true。
    3.传递性:对于任何引用x、y和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)也应该返回true。
    4.一致性:如果x和y引用的对象没有发生变化,那么反复调用x.equals(y)应该返回同样的结果。
    5.非空性:对于任意非空引用x,x.equals(null)应该返回false。

格式:

Java代码  收藏代码
  1. public boolean equals(Object obj) {  
  2.     if(this == obj)  
  3.         return false;  
  4.     if(obj == null)  
  5.         return false;  
  6.     if(getClass() != obj.getClass() )  
  7.         return false;  
  8.     MyClass other = (MyClass)obj;  
  9.     if(str1 == null) {  
  10.          if(obj.str1 != null) {  
  11.               return false;  
  12.          }  
  13.     }else if (!str1.equals(other.str1) )  
  14.              return false;  
  15.      }  
  16.     if(var1 != other.var1)  
  17.         return false;  
  18.     return true;  
  19. }  

 如果子类中增加了新特性,同时保留equals方法,这时比较复杂。

 

 

 

接下来我们通过实例来理解上面的约定。我们首先以一个简单的非可变的二维点类作为开始: 
public class Point{ 
  private final int x; 
  private final int y; 
  public Point(int x, int y){ 
    this.x = x; 
    this.y = y; 
  } 

  public boolean equals(Object o){ 
    if(!(o instanceof Point)) 
      return false; 
    Point p = (Point)o; 
      return p.x == x && p.y == y; 
  } 




假设你想要扩展这个类,为一个点增加颜色信息: 
public class ColorPoint extends Point{ 
  private Color color; 
  public ColorPoint(int x, int y, Color color){ 
    super(x, y); 
    this.color = color; 
  } 

  //override equasl() 

  public boolean equals(Object o){ 
    if(!(o instanceof ColorPoint)) 
     return false; 
    ColorPoint cp = (ColorPoint)o; 
    return super.equals(o) && cp.color==color; 
  } 



  我们重写了equals方法,只有当实参是另一个有色点,并且具有同样的位置和颜色的时候,它才返回true。可这个方法的问题在于,你在比较一个普通点和一个有色点,以及反过来的情形的时候,可能会得到不同的结果: 
public static void main(String[] args){ 
  Point p = new Point(1, 2); 
  ColorPoint cp = new ColorPoint(1, 2, Color.RED); 
  System.out.println(p.equals(cp)); 
  System.out.println(cp.eqauls(p)); 


运行结果: 
true   
false 
这样的结果显然违反了对称性,你可以做这样的尝试来修正这个问题:让ColorPoint.equals在进行“混合比较”的时候忽略颜色信息: 
public boolean equals(Object o){ 
  if(!(o instanceof Point)) 
    return false; 
  //如果o是一个普通点,就忽略颜色信息 
  if(!(o instanceof ColorPoint)) 
    return o.equals(this); 
  //如果o是一个有色点,就做完整的比较 
  ColorPoint cp = (ColorPoint)o; 
  return super.equals(o) && cp.color==color; 


这种方法的结果会怎样呢?让我们先来测试一下: 
public static void main(String[] args){ 
  ColorPoint p1 = new ColorPoint(1, 2, Color.RED); 
  Point p2 = new Point(1, 2); 
  ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE); 
  System.out.println(p1.equals(p2)); 
  System.out.println(p2.equals(p1)); 
  System.out.println(p2.equals(p3)); 
  System.out.println(p1.eqauls(p3)); 


运行结果: 
true 
true 
true 
false 

  这种方法确实提供了对称性,但是却牺牲了传递性(按照约定,p1.equals(p2)和p2.eqauals(p3)都返回true,p1.equals(p3)也应返回true)。要怎么解决呢?

 

事实上,这是面向对象语言中关于等价关系的一个基本问题。要想在扩展一个可实例化的类的同时,既要增加新的特征,同时还要保留equals约定,没有一个简单的办法可以做到这一点。新的解决办法就是不再让ColorPoint扩展Point,而是在ColorPoint中加入一个私有的Point域,以及一个公有的视图(view)方法: 
public class ColorPoint{ 
  private Point point; 
  private Color color; 
  public ColorPoint(int x, int y, Color color){ 
    point = new Point(x, y); 
    this.color = color; 
  } 

  //返回一个与该有色点在同一位置上的普通Point对象 
  public Point asPoint(){ 
    return point; 
  } 

  public boolean equals(Object o){ 
    if(o == this) 
     return true; 
    if(!(o instanceof ColorPoint)) 
     return false; 
    ColorPoint cp = (ColorPoint)o; 
    return cp.point.equals(point)&& 
             cp.color.equals(color); 

  } 


  还有另外一个解决的办法就是把Point设计成一个抽象的类(abstract class),这样你就可以在该抽象类的子类中增加新的特征,而不会违反equals约定。因为抽象类无法创建类的实例,那么前面所述的种种问题都不会发生。 

重写equals方法的要点: 
1. 使用==操作符检查“实参是否为指向对象的一个引用”。

2.判断实参是否为null
3. 使用instanceof操作符检查“实参是否为正确的类型”。 
4. 把实参转换到正确的类型。 
5. 对于该类中每一个“关键”域,检查实参中的域与当前对象中对应的域值是否匹 
  配。对于既不是float也不是double类型的基本类型的域,可以使用==操作符 
  进行比较;对于对象引用类型的域,可以递归地调用所引用的对象的equals方法; 
  对于float类型的域,先使用Float.floatToIntBits转换成int类型的值, 
  然后使用==操作符比较int类型的值;对于double类型的域,先使用 
  Double.doubleToLongBits转换成long类型的值,然后使用==操作符比较 
  long类型的值。 
6. 当你编写完成了equals方法之后,应该问自己三个问题:它是否是对称的、传 
  递的、一致的?(其他两个特性通常会自行满足)如果答案是否定的,那么请找到 
  这些特性未能满足的原因,再修改equals方法的代码。

转自:http://www.iteye.com/topic/269601

        http://huaxia524151.iteye.com/blog/729074

0 0
原创粉丝点击