• equals hashcode toString 方法的使用



    package
    com.wu.toString; import java.util.Date; import java.util.GregorianCalendar; /** * * @author wuyong * @email 382999338@qq.com * @date2016年9月1日下午4:39:09 * 雇员类 */ class Employee { private String name; private double salary;//薪水 private Date hireDay;//入职时间 //构造方法:主要作用是初始化参数 public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); 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; } /* * equals方法:用户检测一个对象是否等于另外一个对象。(non-Javadoc) * 在Object类中,这个方法将判断两个对象是否具有相同的引用。 * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object otherObject) { // a quick test to see if the objects are identical //如果Employee与参数otherObjectd的引用的对象相同,返回true if (this == otherObject) return true; // must return false if the explicit parameter is null //如果参数为空返回的是false if (otherObject == null) return false; // if the classes don't match, they can't be equal //如果Employee与otherObject类型不同,返回false if (getClass() != otherObject.getClass()) return false; // now we know otherObject is a non-null Employee //将otherObject强制转换为Employee类型 Employee other = (Employee) otherObject; // test whether the fields have identical values return name.equals(other.name)//可以写成this.name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay); } /* * ashCode方法:散列码是由对象导出的一个整型值。没有规律(non-Javadoc) * @see java.lang.Object#hashCode() */ public int hashCode() { return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13 * hireDay.hashCode(); } /* * toString方法:用于返回表示对象值得字符串(non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
    package com.wu.toString;
    
    /**
     * 经理类,继承雇员类
     * @author wuyong
     * @email 382999338@qq.com
     * @date2016年9月1日下午4:37:51
     */
    
    class Manager extends Employee
    {  
        private double bonus;//奖金
        
       public Manager(String n, double s, int year, int month, int day)
       {  
          super(n, s, year, month, day);
          bonus = 0;
       }
       //计算薪水
       public double getSalary()
       { 
          double baseSalary = super.getSalary();//调用父类的方法
          return baseSalary + bonus;
       }
    
       public void setBonus(double b)
       {  
          bonus = b;
       }
    
       public boolean equals(Object otherObject)
       {
          if (!super.equals(otherObject)) return false;
          Manager other = (Manager) otherObject; 
          // super.equals checked that this and other belong to the same class
          return bonus == other.bonus;
       }
    
       public int hashCode()
       {   
          return super.hashCode() 
             + 17 * new Double(bonus).hashCode();
       }
    
       public String toString()
       {
          return super.toString()
            + "[bonus=" + bonus
            + "]";
       }
    
       
    }
    package com.wu.toString;
    /**
     * 
     * @author wuyong
     * @email 382999338@qq.com
     * @date2016年9月1日下午4:33:20
     * 测试类
     * 
     */
    import java.util.*;
    
    public class EqualsTest
    {  
       public static void main(String[] args)
       {  
          Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
          Employee alice2 = alice1;
          Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
          Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
    
          System.out.println("alice1 == alice2: " + (alice1 == alice2));
    
          System.out.println("alice1 == alice3: " + (alice1 == alice3));
    
          System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
    
          System.out.println("alice1.equals(bob): " + alice1.equals(bob));
    
          System.out.println("bob.toString(): " + bob);
    
          Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
          Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
          boss.setBonus(5000);
          System.out.println("boss.toString(): " + boss);
          System.out.println("carl.equals(boss): "  + carl.equals(boss));
          System.out.println("alice1.hashCode(): " + alice1.hashCode());
          System.out.println("alice3.hashCode(): " + alice3.hashCode());
          System.out.println("bob.hashCode(): " + bob.hashCode());
          System.out.println("carl.hashCode(): " + carl.hashCode());
       }
    }
  • 相关阅读:
    第一章 前言
    《Linux内核设计与实现》CHAPTER18阅读梳理
    《Linux内核分析》第五周 扒开系统调用的三层皮(下)
    《Linux内核设计与实现》CHAPTER5阅读梳理
    《Linux内核分析》第四周 扒开系统调用的“三层皮”
    《Linux内核设计与实现》CHAPTER1,2阅读梳理
    《Linux内核分析》第三周 构建一个简单的Linux系统MenuOS
    《Linux内核分析》第二周 操作系统是如何工作的?
    《Linux内核分析》第一周 计算机是如何工作的?
    毕业设计 之 一 《构建之法》阅读笔记
  • 原文地址:https://www.cnblogs.com/fifiyong/p/5830625.html
Copyright © 2020-2023  润新知