Java中编写一个完美的equals方法

首先看下Java语言规范对equals方法的要求:

  1,自反性,对于任何非控引用x,x.equals(x)都应该返回true。

  2,对称性,对于任何引用x和y,如果x.equals(y)返回true,那么y.equals(x)也应该返回true。

  3,传递性,如果x.equals(y),y.equals(z)都返回true,那么,x.equals(z)返回true。

  4,一致性,如果x和y引用的对象没有发生变化,那么无论调用多少次x.equals(y)都返回相同的结果。

  5,对于任意非空引用x,x.equals(null)都应该返回false。  

Object类中的equals方法用于检测两个对象是否相等,而相等判断的标准则是二者是否引用同一个对象。

  下面给出编写一个完美的equals方法的原则:

  1,将equals的Object类显式参数命名为otherObject。  

  2,检测otherObject是否与this引用自同一个对象。

  3,检测otherObject是否为null,是的话返回false。

  4,检测this与otherObject是否属于同一个类,这里需要注意的情况就是当比较的两个对象不属于同一个类时,比如p.equals(e),p是一个Person,e是一个Employee,Employee继承自Person。Person拥有name,age私有域,Employee具有occupation域。

  如果相等的概念是的对象的所有域都必须相等,p.equals(e)中使用e instalceof Person返回true,但是如果e.equals(p)调用,p instanceof Employee则返回false,不满足对称性要求,所以这里需要强制采用getClass()来进行类型检测。

  如果相等的概念是由超类决定的,比如认为只要name和age相同,则Person和Employee相等,则可以使用instance进行检测,同时在超类中将该方法定义为final。

  5,将otherObject转换为this类型的变量。

  6,现在开始进行私有数据域的比较,对于基本类型如数值,字符和布尔类型使用==进行比较,对对象域使用equals进行比较,所有域都相同则返回true。

  7,使用@Override声明有助于错误检查。

  示例如下:

 

 1 class Person
 2 {
 3     private String name;
 4     private int age;
 5 
 6     public String getName()
 7     {   
 8         return name;
 9     }   
10     public int getAge()
11     {   
12         return age;
13     }   
14     @Override public boolean equals(Object otherObject)
15     {   
16         if(this == otherObject)
17             return true;
18         if(otherObject == null)
19             return false;        
20        if(getClass() != otherObject.getClass())
21             return false;
22         Person other = (Person)otherObject;
23         return (name.equals(other.name) && (age == other.age));
24     }   
25 }
26 class Employee extends Person
27 {
28     private String occupation;
29 
30     @Override public boolean equals(Object otherObject)
31     {   
32         if(this == otherObject)
33             return true;
34         if(otherObject == null)
35             return false;
36         if(getClass() != otherObject.getClass())
37             return false;
38         if(!super.equals(otherObject))
39             return false;
40         Employee other = (Employee)otherObject;
41         return occupation.equals(other.occupation);
42      }
43 }   

 

posted @ 2014-12-13 16:16  2805101015  Views(2155)  Comments(0Edit  收藏  举报