重载Object类的equals函数

在java中不能由用户重载 “==”操作符,只能重载Object类的equals函数。这里列出了重载时的一般流程和注意事项:(引自Core Java 2)

import java.util.*; class Employee{ public Employee(String n, double s, int year, int month, int day){ name = n; salary = s; calender = new GregorianCalendar(year, month-1, day); hireDay = calender.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireday() { return hireDay; } public void raiseSalary(double byPercent) { salary *= (1 + byPercent/100); } public boolean equals(Employee other){ if( this == other ) return true; if( other == null) return false; //在子类可以继承时,使用getClass()方法: //否则使用final修饰,且使用instanceof if( getClass() != other.getClass()) return false; // if( !(other instanceof Employee)) // return false; return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay); } private String name; private double salary; private Date hireDay; static private GregorianCalendar calender; } class Manager extends Employee{ public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; } public double getSalary() { double base = super.getSalary(); return base + bonus; } public void SetBonus(double b) { bonus = b; } public boolean equals(Employee otherEmployee){ if(super.equals(otherEmployee)) return true; Manager other = (Manager)otherEmployee; return bonus == other.bonus; } private double bonus; } class Saler extends Employee{ public Saler(String n, double s, int year, int month, int day) { super(n, s, year, month, day); scores = 0; } public double getSalary() { double base = super.getSalary(); return base + scores*0.1; } public void SetScores(double s) { scores = s; } public boolean equals(Employee otherEmployee){ if(super.equals(otherEmployee)) return true; Saler other = (Saler)otherEmployee; return scores == other.scores; } private double scores; } public class EmployeeTest { public static void main(String[] args) { Manager boss = new Manager("Carl Cracker", 18000, 2001, 9, 29); boss.SetBonus(5000); Employee[] staff = new Employee[5]; staff[0] = boss; staff[1] = new Employee("Harry Hacker", 8000, 2010, 9, 38); staff[2] = new Employee("Tommy Tester", 7000, 2010, 13, 2); staff[3] = new Saler("Harry Hacker", 8000, 2010, 9, 38); staff[4] = new Saler("Tommy Tester", 7000, 2010, 13, 2); for(Employee e : staff) System.out.println("/nName: " + e.getName() + "/nSalary: " + e.getSalary() + "/nHireDay: " + e.getHireday()); if(staff[1].equals(staff[3])) System.out.println(staff[1].getName() + "==" + staff[3].getName()); if(staff[2].equals(staff[4])) System.out.println(staff[2].getName() + "==" + staff[4].getName()); } }  

在这里主要关心父类(Employee)和两个子类(Manager和Saler)的equals函数,可见在父类中定义equals函数主要有5步(已在代码中标出),在子类中则有两步。一般这些步骤最好都不要省略。

这里有一点需要注意:即在step3有两个方案,这两个方案到底选择哪个有一个准则可供参考。

 

> 若子类需要自己的equals函数,为了满足“相等”的对称性,必须使用getClass();

> 若父类已经完全决定了equals的定义,使用instanceof。且对equals使用final修饰

 

另外,在Java中要求若x.equals(y)成立,则x.hashCode() == y.hashCode(),Object中默认的hashCode返回的是对象的内存地址,不同的对象一般不同,所以在定义了equals函数后不要忘记同时定义hashCode函数。常用的方案如下:

//针对以上的Employee类,可以定义如下的hashCode() public int hashCode() { return 7*name.hashCode() + 11* new Double(salary).hashCode() + 13*hireDay.hashCode(); } 

 

你可能感兴趣的:(java,object,manager,String,equals,Class)