Java判断两个对象是否相等的规则

       Object类中的equals方法用于检测一个对象是否等于另外一个对象。在Object类中,这个方法将判断两个对象是否具有相同的引用。如果两个对象具有相同的引用,它们一定是相等的。从这点上看,将其作为默认操作也是合情合理的。然而对于多数类来说,这种判断并没有什么意义。例如,采用这种方式比较两个PrintStream对象是否相等就完全没有意义。然而,经常需要检测两个对象状态的相等性,如果两个对象的状态相等,就认为这两个对象是相等的。

       例如,如果两个雇员对象的姓名、薪水和雇佣日期都一样,就认为它们是相等的(在实际的雇员数据库中,比较id更有意义,利用下面这个示例演示equals方法的实现机制)。

public boolean equals(Object otherObject)
    {
        Employee other = (Employee)otherObject;
        //a quick test to see if the objects are identical
        if (this == otherObject) return true;

        //must reture false if the explicit parameter is null
        if (otherObject == null) return  false;

        //if the classes don't match, they can't be equal
        if (getClass() != otherObject.getClass())
            return false;
        
        //test whether the fields have identical values
        return name.equals(other.name)
                && salary== other.salary
                && hireDay.equals(other.hireDay);
    }

getClass方法将返回一个对象所属的类。在检测中,只有在两个对象属于同一个类时,才有可能相等。

      Java语言规范要求equals方法具有下面的特性:

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

   2)对称性:对于任何引用x和y,当且仅当y.equals(x)返回true,x.equals(y)也应该返回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。

以下是编写完美的equals方法的建议:

    1、显式参数命名为otherObject,稍后需要将它转换成另一个叫做other的变量。

    2、检测this与otherObject是否引用同一个对象:

         if(this == otherObject) return true;

         这条语句只是一个优化。实际上,这是一种经常采用的形式。因为计算这个等式要比一个一个地比较类中的域所付出的代价要小的             多。

     3、检测otherObject是否为null,如果为null,返回false。这项检测是很必要的。

          if(otherObject == null) return false;

     4、比较this与otherObject是否属于同一个类。如果 equals的语义在每个子类中有所改变,就使用getClass检测:

          if(getClass()!=otherObject.getClass()) return false;

          如果所有的子类都拥有统一的语义,就使用instanceof检测:

           if(!(otherObject instanceof ClassName) ) return false;

     5、将otherObject转换为相应的类类型变量:

           ClassName other = (ClassName)otherObject;

     6、现在开始对所有需要比较的域进行比较了。使用==比较基本类型域,使用equals比较对象域。如果所有的域都匹配,就返回true;否            则返回false。

          return field1 == other.field1 && Object.equals(field2, other,field2)&&...

 如果在子类中重新定义equals,就要在其中包含调用super.equals(other).

以下以一个员工类和经理类来说明equals方法

       

public class Employee {
    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
        this.hireDay = calendar.getTime();
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public Date getHireDay() {
        return hireDay;
    }

    public void raiseSalary(double byPercent)
    {
        double raise = salary * byPercent /100;
        salary += raise;
    }
    @Override
    public boolean equals(Object otherObject)
    {
        Employee other = (Employee)otherObject;
        //a quick test to see if the objects are identical
        if (this == otherObject) return true;

        //must reture false if the explicit parameter is null
        if (otherObject == null) return  false;

        //if the classes don't match, they can't be equal
        if (getClass() != otherObject.getClass())
            return false;

        //test whether the fields have identical values
        return Objects.equals(name, other.name)
                && salary == other.salary
                && Objects.equals(hireDay, other.hireDay);

    }

    @Override
    public int hashCode(){
        return Objects.hash(name, salary, hireDay);
    }

    @Override
    public String toString() {
        return getClass().getName()+"{" + "name='" + name + '\'' + ", salary=" + salary + ", "
                + "hireDay=" + hireDay + '}';
    }
}

     

public class Manager extends Employee {

    private double bonus;

    public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        this.bonus = 0;
    }

    public double getSalary()
    {
        double baseSalary = super.getSalary();
        return baseSalary + bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public boolean equals(Object o) {
        if (!super.equals(o)) return false;
        Manager other = (Manager) o;
        return bonus == other.bonus;
    }

    @Override
    public int hashCode() {
        return super.hashCode() + 17 * new Double(bonus).hashCode();
    }

}

 

 

 

     

 

posted @ 2018-02-14 13:55  PopeZhang  阅读(485)  评论(0)    收藏  举报