zoukankan      html  css  js  c++  java
  • 重写java equals 方法的建议

    @Override
    public boolean equals(Object otherObject) {
    //1.检测this 与 otherObject 是否引用同一个对象
    if (this == otherObject) return true;
    //2.检测otherObject 是否为null, 如果为null,返回false
    if (null == otherObject) return false;
    //3.比较this 与 ohterObject 是否属于同一个类
    if(getClass() != otherObject.getClass()) return false;

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

    //4. 将otherObject 转换为相应的类类型变量
    Employee employee = (Employee) otherObject;

    //5.域比较
    if (id != employee.id) return false;
    if (Double.compare(employee.salary, salary) != 0) return false;
    if (name != null ? !name.equals(employee.name) : employee.name != null) return false;

    return true;
    }

    -- 摘《java核心技术:卷一》
  • 相关阅读:
    IP 排序
    React 项目搭建
    Nuxt
    element table 封装
    iviewui Slider 滑块的坑
    浏览器自动填写用户名和密码
    asd
    正则表达式
    snmp中载入第三方mib库(转载)
    一键lamp
  • 原文地址:https://www.cnblogs.com/lyzblog/p/6013297.html
Copyright © 2011-2022 走看看