以下通过实际例子对类创建过程汇中常用的equals()、hashcode()、toString()方法进行展示,三个方法的创建过程具有通用性,在项目中可直接改写。
//通过超类Employee和其子类Manager ,验证在类继承过程中equals()、hashcode()、toString()方法的使用.
package equals;
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.toStirng():"+bob);//使用toString()方法和输出bob对象的字符串形式
//创建子类Manager的对象变量
Manager carl=new Manager("Carl Cracker",80000,1987,12,15);
Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
boss.setBluns(5000);
System.out.println("boss.toString():"+boss);
System.out.println("carl.equals(boss):"+carl.equals(boss));
System.out.println("alice1.hashCode():"+alice1.hashCode());
//调用hashcode()方法导出对象的散列码
System.out.println("alice3.hashCode():"+alice3.hashCode());
System.out.println("boss.hashCode():"+boss.hashCode());
System.out.println("carl.hashCode():"+carl.hahsCode());
}
}
//创建超类Employee
package equals;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;
public 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;
}
//创建方法raiseSalary()
public void raiseSalary(double byPercent){
double raise=salary*byPercent/100;
salary+=raise;
}
//创建方法equals()
public boolean equals(Object otherObject){
if(this==otherObject){//检测this和otherObject是否引用同一对象
return true;
}
if(otherObject==null){//检测显式参数是否为null,是则返回false
return false;
}
if(getClass()!=otherObject.getClass()){
//比较this和otherObject是否属于同一个类,如果两者不属于同一个类,则不能进行equals()操作
return false;
}
//将otherObject转换成相应的类类型变量
Employee other=(Employee)otherObject;
//现在开始对所需要比较的域进行比较
return Objects.equals(name,other.name)
&&Objects.equals(salary,other.salary)//&&salary==other.salary这种写法是否可行?
&&Objects.equals(hireDay,other.hireDay);
}
public int hashCode(){
//Objects.hash()方法提供多个参数,
//对各个参数调用Objects.hashCode()方法获得各自的散列值,并组合这些散列值。
return Objects.hash(name,salary,hireDay);
}
public String toString(){
return getClass().getName()+"[name"+name+"salary="+salary+"hireDay="+hireDay+"]";
}
}
//创建Employee子类Manager
package equals;
public class Manager extends Employee{
private double bouns;//创建子类自身新的的实例域
public Manager(String n,double s,int year.int month,int day){
super(n,s,year,month,day);//调用超类的构造函数
bouns=0;
}
public double getSalary(){//覆写超类Employee的getSalary()方法
double baseSalary=super.getSalary();//调用超类的getSalary()
return baseSalary+bouns;
}
public void setBouns(double a){
bouns=a;
}
public boolean equals(Object otherObject){//覆写超类Employee的equals()方法
if(!super.equals(otherObject)){
return false;
}
Manager other=(Manager)otherObject;
return bouns==other.bouns;
}
public int hashCode(){
return super.hashCode()+17*new Double(bouns).hashCode();
}
public String toString(){
return super.toString()+"[bouns="+bouns+"]";
}
}