Object类
此文档为java的Object类学习笔记
一、介绍Object类的几个方法
Object类是java所有类的始祖,在java中每个类都是由它扩展而来。可以用Object类型变量引用任何类型的对象
Object obj = new Employee("bob", 40000);
Employee e = (Employee)obj;
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
欢迎转载,欢迎错误指正与技术交流,欢迎交友谈心
文章标题:Object类
文章字数:798
本文作者:Brain Cao
发布时间:2017-06-29, 11:35:57
最后更新:2020-03-15, 16:40:46
原始链接:https://braincao.cn/2017/06/29/java-object-class/版权声明:本文为博主原创文章,遵循 BY-NC-SA 4.0 版权协议,转载请保留原文链接与作者。