Object所有類的超類之幾個方法詳析

Pop_Rain發表於2017-06-29

Object類是java所有類的始祖,在java中每個類都是由它擴充套件而來。可以用Object型別變數引用任何型別的物件

Object obj = new Employee("bob", 40000);
Employee e = (Employee)obj;

一、介紹幾種Object類的方法

toString方法

toString返回表示物件值的字串

hash與hashCode方法

Objects.hash(Object... objs)產生並返回一個雜湊碼。Objects.hash(name, age, hireDay)根據引數產生一個雜湊碼
Object.hashCode()檢視返回物件已經存在的雜湊碼,每個物件都有一個預設的雜湊碼,其值為物件的儲存地址

equals方法

如果重新定義equals方法,就必須重新定義hashCode方法(e.g.String類重新定義hashCode方法,使得相同內容的字串都有相同的hashCode),以便使用者可以將物件插入到雜湊表中
equals與hashCode的定義必須一致:如果x.equals(y)返回true,那麼x.hashCode()就必須與y.hashCode()具有相同的值;再比如,如果用定義的Employee.equals方法比較僱員的ID,那麼hashCode方法就只需要雜湊ID,而不是僱員的姓名或儲存地址

二、上述幾個方法的助理解示例程式碼

import java.util.*;

public class Test
{
	public static void main(String args[])
	{
		Employee e1 = new Employee("Alice", 75000, 1987, 12, 15);		
		Employee e2 = e1;
		Employee e3 = new Employee("Alice", 75000, 1987, 12, 15);
		
		Employee b1 = new Employee("Bob", 50000, 1989, 10, 1);
		
		System.out.println("e1 == e2: " + (e1==e2));
		System.out.println("e1 == e3: " + (e1==e3));
		System.out.println("e1.equals(e2): " + e1.equals(e2));
		System.out.println("e1.equals(e3): " + e1.equals(e3));
		System.out.println("e1.hashCode(): " + e1.hashCode());
		System.out.println("e3.hashCode(): " + e3.hashCode());
		System.out.println("e1.equals(b1): " + e1.equals(b1));
		System.out.println("b1.toString(): " + b1.toString());
		System.out.println("b1.hashCode(): " + b1.hashCode());
		
		Manager carl = new Manager("carl", 80000, 1987, 12, 15);
		Manager boss = new Manager("carl", 80000, 1987, 12, 15);
		boss.setBonus(5000);
//		carl.setBonus(5000);
		
		System.out.println("boss.toString(): " + boss.toString());
		System.out.println("carl.equals(boss): " + carl.equals(boss));
		System.out.println("carl.hashCode(): " + carl.hashCode());
		System.out.println("boss.hashCode(): " + boss.hashCode());
		
	}
}

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 cal = new GregorianCalendar(year, month-1, day);
		hireDay = cal.getTime();
	}
	
	public String getName()
	{
		return name;
	}
	
	public double getSalary()
	{
		return salary;
	}
	
	public Date getHireDay()
	{
		return hireDay;
	}
	
	public void raiseSalary(double percent)
	{
		salary *= (1 + percent/100);
	}
	
	public boolean equals(Object obj)
	{
		if(this == obj)
			return true;
		if(this == null)
			return false;
		if(this.getClass() != obj.getClass())
			return false;
		
		Employee tmp = (Employee)obj;
		return Objects.equals(name, tmp.name) && this.salary==tmp.salary && hireDay.equals(tmp.hireDay);
	}
	
	public int hashCode()
	{
		return Objects.hash(name, salary, hireDay);
	}
	
	public String toString()
	{
		return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
	}
}

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 void setBonus(double b)
	{
		bonus = b;
	}
	
	public double getSalary()
	{
		return super.getSalary() + bonus;
	}
	
	public boolean equals(Object obj)
	{
		if(!super.equals(obj))
			return false;
		Manager m = (Manager)obj;
		return bonus == m.bonus;
	}
	
	public int hashCode()
	{
		return super.hashCode() + 17*new Double(bonus).hashCode();
	}
	
	public String toString()
	{
		return super.toString() + "[bonus=" + bonus + "]";
	}
}
output:
e1 == e2: true
e1 == e3: false
e1.equals(e2): true
e1.equals(e3): true
e1.hashCode(): -270951486
e3.hashCode(): -270951486
e1.equals(b1): false
b1.toString(): test.Employee[name=Bob,salary=50000.0,hireDay=Sun Oct 01 00:00:00 CST 1989]
b1.hashCode(): -133040315
boss.toString(): test.Manager[name=carl,salary=80000.0,hireDay=Tue Dec 15 00:00:00 CST 1987][bonus=5000.0]
carl.equals(boss): false
carl.hashCode(): 1908734458
boss.hashCode(): -1112473094

相關文章