07-06
06
相等测试与继承
作者:Java伴侣 日期:2007-06-06
下面的例子,利用JAVA的多态性,重写了equals(),hashCode(),toString()三个方法。功能:测试多个类比较相等,利用重写的toString()和hashCode()查看每个类中的详细信息。
import java.util.Date;
import java.util.GregorianCalendar;
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 java.util.GregorianCalendar(year,
month - 1, day);
hireDay = calendar.getTime();
}
public Date getHireDay() {
return hireDay;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary = raise;
}
//重写equals()
public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (otherOb == null) {
return false;
}
if (this.getClass() != otherOb.getClass()) {
return false;
}
Employee other = (Employee) otherOb;
return name.equals(other.name) && salary == other.salary &&
hireDay.equals(other.hireDay);
}
//重写hashCode()
public int hashCode() {
return 7 * name.hashCode() + 11 * new Double(salary).hashCode() +
13 * hireDay.hashCode();
}
//个性化toString()
public String toString() {
return getClass().getName()
+ "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay +
"]";
}
}
Manager 继承 Employee,构造器中super基类参数,曾经bonus额外收入
public 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 static void main(String[] args) {
}
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重写equals()
public boolean equals(java.lang.Object otherOb) {
if (!super.equals(otherOb)) {
return false;
}
Manager other = (Manager) otherOb;
return bonus == other.bonus;
}
//重写hashCode
public int hashCode() {
return super.hashCode()
+ 17 * new Double(bonus).hashCode();
}
//实现父类toString增加bonus
public String toString() {
return super.toString() + "[bonus=" + bonus + "]";
}
}
Test类中测试以上两个类:
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class EqualsTest {
public static void main(String[] args) {
EqualsTest equalstest = new EqualsTest();
Employee alice1 = new Employee("jingjing", 7500, 1987, 12, 15);
Employee alice2 = alice1;
Employee alice3 = new Employee("jingjing", 7500, 1987, 12, 15);
Employee bob = new Employee("Zhangfan", 5000, 1989, 10, 1);
//user ==
System.out.println("alicel==alice2:" + (alice1 == alice2));
System.out.println("alicel==alice3:" + (alice1 == alice3));
//user equals
System.out.println("alice1.equals(alice3):" + alice1.equals(alice3));
System.out.println("alice1.equals(bob):" + alice1.equals(bob));
System.out.println("bob.toString():" + bob);
//S.OP(bob)先调用Object的toString().但该方法被子类重构了,所以相当于bob.toString()
Manager carl = new Manager("Carl Cracker", 8000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 8000, 1987, 12, 15);
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("bob.hashCode():" + bob.hashCode());
System.out.println("carl.hashCode():" + carl.hashCode());
}
}
运行结果:
alicel==alice2:true
alicel==alice3:false
alice1.equals(alice3):true
alice1.equals(bob):false
bob.toString():Employee[name=Zhangfan,salary=5000.0,hireDay=Sun Oct 01 00:00:00 GMT 1989]
boss.toString():Manager[name=Carl Cracker,salary=8000.0,hireDay=Tue Dec 15 00:00:00 GMT 1987][bonus=5000.0]
carl.equals(boss):false
alice1.hashCode():-865196105
alice3.hashCode():-865196105
bob.hashCode():-596912690
carl.hashCode():723224256
此题源于P166页
复制内容到剪贴板 程序代码
import java.util.Date;
import java.util.GregorianCalendar;
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 java.util.GregorianCalendar(year,
month - 1, day);
hireDay = calendar.getTime();
}
public Date getHireDay() {
return hireDay;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary = raise;
}
//重写equals()
public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (otherOb == null) {
return false;
}
if (this.getClass() != otherOb.getClass()) {
return false;
}
Employee other = (Employee) otherOb;
return name.equals(other.name) && salary == other.salary &&
hireDay.equals(other.hireDay);
}
//重写hashCode()
public int hashCode() {
return 7 * name.hashCode() + 11 * new Double(salary).hashCode() +
13 * hireDay.hashCode();
}
//个性化toString()
public String toString() {
return getClass().getName()
+ "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay +
"]";
}
}
Manager 继承 Employee,构造器中super基类参数,曾经bonus额外收入
复制内容到剪贴板 程序代码
public 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 static void main(String[] args) {
}
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重写equals()
public boolean equals(java.lang.Object otherOb) {
if (!super.equals(otherOb)) {
return false;
}
Manager other = (Manager) otherOb;
return bonus == other.bonus;
}
//重写hashCode
public int hashCode() {
return super.hashCode()
+ 17 * new Double(bonus).hashCode();
}
//实现父类toString增加bonus
public String toString() {
return super.toString() + "[bonus=" + bonus + "]";
}
}
Test类中测试以上两个类:
复制内容到剪贴板 程序代码
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class EqualsTest {
public static void main(String[] args) {
EqualsTest equalstest = new EqualsTest();
Employee alice1 = new Employee("jingjing", 7500, 1987, 12, 15);
Employee alice2 = alice1;
Employee alice3 = new Employee("jingjing", 7500, 1987, 12, 15);
Employee bob = new Employee("Zhangfan", 5000, 1989, 10, 1);
//user ==
System.out.println("alicel==alice2:" + (alice1 == alice2));
System.out.println("alicel==alice3:" + (alice1 == alice3));
//user equals
System.out.println("alice1.equals(alice3):" + alice1.equals(alice3));
System.out.println("alice1.equals(bob):" + alice1.equals(bob));
System.out.println("bob.toString():" + bob);
//S.OP(bob)先调用Object的toString().但该方法被子类重构了,所以相当于bob.toString()
Manager carl = new Manager("Carl Cracker", 8000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 8000, 1987, 12, 15);
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("bob.hashCode():" + bob.hashCode());
System.out.println("carl.hashCode():" + carl.hashCode());
}
}
运行结果:
引用内容
alicel==alice2:true
alicel==alice3:false
alice1.equals(alice3):true
alice1.equals(bob):false
bob.toString():Employee[name=Zhangfan,salary=5000.0,hireDay=Sun Oct 01 00:00:00 GMT 1989]
boss.toString():Manager[name=Carl Cracker,salary=8000.0,hireDay=Tue Dec 15 00:00:00 GMT 1987][bonus=5000.0]
carl.equals(boss):false
alice1.hashCode():-865196105
alice3.hashCode():-865196105
bob.hashCode():-596912690
carl.hashCode():723224256
此题源于P166页
评论: 0 | 引用: 0 | 查看次数: 763
发表评论