import java.util.*;
//本程序演示的是:Equals() 方法 ,hashcode()方法,toString() 方法
public class EqualsTest
{
public static void main(String [] args)
{
Employee alice1=new Employee("Alice Adams",75000,2010,12,15);
Employee alice2=alice1;
Employee alice3=new Employee("Alice Adams",75000,2010,12,15);
Employee bob=new Employee("Bob Dyln",50000,2010,10,15);
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("SpringLee",100000,2022,5,6);
Manager boss=new Manager("SpringLee",100000,2022,5,6);
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());
}
}
class Employee
{
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;
}
public boolean equals(Object otherObject)
{
//快速测试 是否对象是同一个对象
if(this==otherObject) return true;
//如果传入的参数是空的,那么就返回false
if(otherObject==null)return false;
//如果类都不匹配,那么他们不可能相等
if(getClass()!=otherObject.getClass())return false;
//现在我们知道了otherObject是一个非空的Employee
Employee other=(Employee) otherObject;
//测试是否字段的值也是相等的
return name.equals(other.name) && salary==other.salary && hireDay.equals(other.hireDay);
}
public int hashCode()
{
return 7*name.hashCode()+11* new Double(salary).hashCode()+13*hireDay.hashCode();
}
public String toString()
{
return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
private String name;
private double salary;
private Date hireDay;
}
class Manager extends Employee
{
/*
name
salary
hire year
month
day
*/
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 检查 这个和另外的对象 是属于同一个类
return bonus==other.bonus;
}
public int hashCode()
{
return super.hashCode()+17*new Double(bonus).hashCode();
}
public String toString()
{
return super.toString()+"[bonus="+bonus+"]";
}
private double bonus;
}
运行结果:
C:\Users\86133\.jdks\openjdk-18.0.1\bin\java.exe "-javaagent:D:\Program Files\ideaIC-2022.1.win\lib\idea_rt.jar=16282:D:\Program Files\ideaIC-2022.1.win\bin" -Dfile.encoding=UTF-8 -classpath D:\eCode\Java\CoreJavaVolumeI_Fundamentals\Example5-3EqualsTestAgain\out\production\Example5-3EqualsTestAgain EqualsTest
alice1==alice2:true
alice1==alice3:false
alice1.equals(alice3):true
alice1.equals(bob):false
bob.toString():Employee[name=Bob Dyln,salary=50000.0,hireDay=Fri Oct 15 00:00:00 CST 2010]
boss.toSTring():Manager[name=SpringLee,salary=100000.0,hireDay=Fri May 06 00:00:00 CST 2022][bonus=5000.0]
carl.equals(boss):false
alice1.hashCode():337832952
alice3.hashCode():337832952
bob.hashCode():638954909
carl.hashCode():-209241639
进程已结束,退出代码0