package mqiano.com.cnblogs;
import java.util.Date;
import java.util.GregorianCalendar;
import static java.lang.System.out;
public class Test {
public static void main(String[] argu) {
Manager mqiano = new Manager("mqiano", 10000, 2012, 7, 0);
Manager mqianoo = new Manager("mqiano", 10000, 2012, 7, 0);
out.println(mqiano.equals(mqianoo));
}
}
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 raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
final private String name;
private double salary;
private Date hireDay;
@Override
public boolean equals(Object otherObject) {
// a quick test to see if the objects are identical
if (this == otherObject)
return true;
if (otherObject == null)
return false;
if (this.getClass() != otherObject.getClass())
return false;
Employee other = (Employee) otherObject;
out.println("super's getClass()=" + this.getClass());
out.println("And otherObjec's getClass()=" + otherObject.getClass());
return name.equals(other.name) && salary == other.salary
&& hireDay.equals(other.hireDay);
}
}
class Manager extends Employee {
/**
* @param n
* the employee's name
* @param s
* the salary
* @param year
* the hire year
* @param month
* the hire month
* @param day
* the hire day
*/
public Manager(String n, double s, int year, int month, int day) {
super(n, s, year, month, day);
bonus = 0;
}
public double getSalary(int b) {
double baseSalary = super.getSalary();
return baseSalary + bonus + b;
}
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double b) {
bonus = b;
}
@Override
public boolean equals(Object otherObject) {
if (!super.equals(otherObject))
return false;
Manager other = (Manager) otherObject;
return bonus == other.bonus;
}
private double bonus;
}
子类equals()方法里调用super.equals(),super内的函数的执行也是动态绑定。