zoukankan      html  css  js  c++  java
  • Java:Object类的equals()方法 如何编写一个完美的equals()方法

    一  代码实例:

    package freewill.objectequals;  
    /** 
    * @author freewill 
    * @see Core Java page161 
    * @desc getClass实现方式,另有instance of实现方式,根据不同场景使用。 
    */  
    public class Employee {  
        private String name;  
        private int salary;  
        private String hireDay;  
      
        public String getName() {  
            return name;  
        }  
      
        public void setName(String name) {  
            this.name = name;  
        }  
      
        public int getSalary() {  
            return salary;  
        }  
      
        public void setSalary(int salary) {  
            this.salary = salary;  
        }  
      
        public String getHireDay() {  
            return hireDay;  
        }  
      
        public void setHireDay(String hireDay) {  
            this.hireDay = hireDay;  
        }  
      
        @Override  
        public boolean equals(Object obj) {  
            // a quick test to see if the objects are identical  
            if (this == obj)  
                return true;  
            // must return false if the explicit parameter is null  
            if (obj == null)  
                return false;  
            // if the class don't match,they can't be equal  
            if (getClass() != obj.getClass())  
                return false;  
            // now we know obj is non-null Employee  
            Employee other = (Employee) obj;  
            // test whether the fileds have identical values  
            return name.equals(other.name) && salary == other.salary  
                    && hireDay.equals(other.salary);  
        }  
      
    }  
    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,如果是,则返回false,

    if(otherObject==null){

              return false ;

    }

    4 比较this和otherObject是否属于同一个类,如果equals的语义在每个子类中有所改变,就是用getClass()方法检测;

    if(getClass()!=otherObject.getClass()){

    return false;

    }

    如果所有的子类有统一的语义,则使用instanceof检测;

    if(!(otherObject isntanceof ClassName)){

    return false;

    }

    5 将otherObject转换成相应的类类型变量;

    ClassName other=(ClassName)otherObject;

    6 现在开始对所需要比较的域进行比较,

        (1)使用==比较基本类型域,

         (2)使用equals比较对象域,

    如果所有的域都匹配,则返回true,否则返回false。

    格式如下:

    return field1==other.field1

    &&Objects.equals(field2,other.field2)

    &&...........;

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

     

     

  • 相关阅读:
    my34_脚本冥等添加自动任务-mysql监控部署
    zabbix4.2 安装
    面试题12:字符串无重复子串的最大长度
    面试题11:二叉树的非递归前、中、后、层级遍历
    面试题10:二叉树的最大路径和
    面试题9:数组堆化、堆的插入、堆的删除、堆排序
    面试题8:无序数组的最大差值
    面试题7:判断链表是否有环,返回环的入口点
    面试题6:二叉树最近公共节点(LCA)《leetcode236》
    面试题6:二叉树转单链表
  • 原文地址:https://www.cnblogs.com/dengyungao/p/7525143.html
Copyright © 2011-2022 走看看