美文网首页
Object类hashCode

Object类hashCode

作者: 爱做梦的严重精神病患者 | 来源:发表于2018-10-19 15:31 被阅读0次

    1.hashCode()方法

    散列码(hash code)是由对象导出的一个整型值。散列码是没有规律的。hashCode方法定义在Object类中,每个对象都有一个默认的散列码,其值为对象的存储地址
     如果重新定义equals方法,就必须重新定义hashCode方法,以便用户可以将对象插入散列表中
    equals与hashCode定义必须一致:如果x.equals(y)返回true,那么x.hashCode()就必须与y.hashCode()具有相同的值
     例如,如果用定义的Employee.equals比较雇员的ID,那么hashCode方法就需要散列ID,而不是雇员的名字或存储地址。

    2.toString()方法

    toString()方法用于返回表示对象值的字符串。绝大多数的toString方法都遵循这样的格式类的名字,随后是一对方括号括起来的域值
     随处可见toString方法的主要原因:只要对象与一个字符串通过操作符"+"连接起来,Java编译就会自动地调用toString方法,以便获得这个对象的字符串描述。
     强烈建议为自定义的每一个类增加toString方法。

    public class Employee {
        private String name;
        private double salary;
        private LocalDate hireDay;
    
        public Employee(String name, double salary, int year, int month, int day) {
            this.name = name;
            this.salary = salary;
            hireDay = LocalDate.of(year, month, day);
        }
    
        public String getName() {
            return name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public LocalDate getHireDay() {
            return hireDay;
        }
        
        public void raiseSalary(double byPercent) {
            double raise = salary * byPercent / 100;
            salary += raise;
        }
        
        @Override
        public boolean equals(Object otherObject) {
           if (this == otherObject) return true;
    
           if (otherObject == null) return false;
    
           if (getClass() != otherObject.getClass()) return false;
    
           Employee other = (Employee) otherObject;
    
           return Objects.equals(name, other.name) && salary == other.salary
                   && Objects.equals(hireDay, other.hireDay);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name, salary, hireDay);
           另一种写法 /*return 7 * Objects.hashCode(name)
                    + 11 * Double.hashCode(salary)
                    + 13 * Objects.hashCode(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);
            bonus = 0;
        }
    
        @Override
        public double getSalary() {
            double baseSalary = super.getSalary();
            return baseSalary + bonus;
        }
    
        public void setBonus(double bonus) {
            this.bonus = bonus;
        }
    
        @Override
        public boolean equals(Object otherObject) {
            if (!super.equals(otherObject)) return false;
            Manager other = (Manager) otherObject;
            return bonus == other.bonus;
        }
    
        @Override
        public int hashCode() {
          与equals对应  return super.hashCode() + 17 * Double.hashCode(bonus);
        }
    
        @Override
        public String toString() {
            return super.toString() + "[bonus=" + bonus + "]";
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Object类hashCode

          本文链接:https://www.haomeiwen.com/subject/zoiyzftx.html