Object所有類的超類之幾個方法詳析
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
相關文章
- 【Java面試題】之Object類中方法詳解Java面試題Object
- Java Object類的各個方法JavaObject
- Java中常用類之Object類JavaObject
- 【轉】Object類的常用方法Object
- Java™ 教程(Object作為超類)JavaObject
- Java中Object類的常用方法JavaObject
- Object類和String類equals方法的區別Object
- Object類有哪些公用方法?Object
- java之Object類介紹JavaObject
- JavaBean重寫Object類中的方法JavaBeanObject
- 關於字串分類抽取的幾個方法字串
- object類Object
- Java常用類-Object類JavaObject
- String類中的equals方法與Object類中的equals方法的不同點Object
- Java Thread 類相關的幾個核心方法Javathread
- JAVA中object類中toString()方法JavaObject
- JAVA Object類JavaObject
- 看基類被那幾個類了
- 不是所有 Response 類都有 header () 方法Header
- Java面試系列第2篇-Object類中的方法Java面試Object
- Java中Class類與Object類之間有什麼關係?JavaObject
- Object類toString()Object
- Object類物件剖析Object物件
- 介紹幾個好用的工具類
- Extjs DOM操作的幾個類JS
- IOS幾個常用類庫的用法iOS
- 推薦幾個開源類庫,超好用,遠離996!996
- PHP中把stdClass Object轉array的幾個方法PHPObject
- java 所有的集合類以及Apache的集合類???JavaApache
- 類似資料字典的幾個表
- 常見物件-Object類物件Object
- Object類-equals()、==、hashCode()Object
- 內部類,Object,字串Object字串
- Swift 幾種動態判斷類的方法Swift
- Java超類與子類Java
- JAVA集合類簡要筆記 - 內部類 包裝類 Object類 String類 BigDecimal類 system類Java筆記ObjectDecimal
- Java File 類的使用方法詳解Java
- php陣列的分類有哪幾個PHP陣列