您的位置:首页 > 编程语言 > Java开发

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

2014-12-13 16:16 405 查看
首先看下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声明有助于错误检查。

  示例如下:

class Person
{
private String name;
private int age;

public String getName()
{
return name;
}
public int getAge()
{
return age;
}
@Override public boolean equals(Object otherObject)
{
if(this == otherObject)
return true;
if(otherObject == null)
return false;
if(getClass() != otherObject.getClass())
return false;
Person other = (Person)otherObject;
return (name.equals(other.name) && (age == other.age));
}
}
class Employee extends Person
{
private String occupation;

@Override public boolean equals(Object otherObject)
{
if(this == otherObject)
return true;
if(otherObject == null)
return false;
if(getClass() != otherObject.getClass())
return false;
if(!super.equals(otherObject))
return false;
Employee other = (Employee)otherObject;
return occupation.equals(other.occupation);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐