import java.util.*;
public class EqualsTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
Employee alice2 = alice1;
Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
/**相等测试*/
//用“==”判断两个对象地址是否相同
System.out.println("alice1 == alice2:" + (alice1 == alice2));
System.out.println("alice1 ==alice3:" + (alice1 == alice3));
//用Object.equals()方法判断两对象是否为同一对象
System.out.println("alice1.equals(alice3):" + (alice1.equals(alice3)));
System.out.println("alice1.equals(bob):" + alice1.equals(bob));
/**ToString 方法*/
System.out.println("bob.toString():" + bob);
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
/**equals()方法和Tostring()方法,hashCode()方法,继承测试*/
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("carl.hashCode(): "+carl.hashCode());
System.out.println("bob.hashCode(): "+bob.hashCode());
}
}
/**
* 数值域:
* 姓名:name
* 工资:salary
* 日期;hireDay
* 方法域:
* getName()
* setSalary()
* getHireDay()
* rauseSalary()
* equals()
* haseCode()
* */
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 rauseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
public boolean equals(Object otherObject) {
// a quick test to see if the objects are identica
if (this == otherObject)
return true;
// must return false if the explicit parameter id null
if (otherObject == null)
return false;
// if the class don't match ,they can't be equal
if (getClass() != otherObject.getClass())
return false;
// now we konw otherObject is a non-null Employee
Employee other = (Employee) otherObject;
// test whether the fields hava identical values
return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);
}
public int haseCode() {
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;
}
/**
* 经理:继承员工类
* 添加成员变量
* bonus*/
class Manager extends Employee {
public Manager(String n, double s, int year, int month, int day) {
super(n, s, year, month, day);
bonus = s;
}
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 checked that this and other belong ato the same calss
return bonus == other.bonus;
}
public int hasehCode() {
return super.hashCode() + 17 * new Double(bonus).hashCode();
}
public String toString() {
return super.toString() + "[bonus=" + bonus + "]";
}
private double bonus;
}
Object类中的equals方法用于检测一个对象是否等于另一个对象,Object类中,判断了两个对象是否有相同引用。
equals方法具有自反性、对称性、传递性、一致性。
getClass方法返回一个对象所述的类。
hashCode方法,字符串的散列码是有内容导出的,StringBuffer类中没有定义hashCode方法,它的散列码是Object类的默认
hashCode方法导出的对象存储地址。
java.lang.Object
Internet hashCode()返回对象的散列码。散列码可以是任意的证整数,包括正数或负数。两个相等的对像返回相等的散列码。
java.util.Arrays
static Int hashCode(type[] a)
计算数组a的散列码。组成这个数组的元素类型可是object、Int、long.short、char.byte、boolen、flaot
或doubleEqualsTest.java