package inheritance;
//Manager类继承了Employee类
public class Manager extends Employee{
private double bonus;
public Manager(String n, double s, int year, int month, int day){
//利用super关键词调用Employee类的构造器
super(n, s, year, month, day);
bonus = 0;
}
//覆盖了Employee类中的getSalary方法
public double getSalary(){
//用super关键字调用Employee类的方法
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double b){
bonus = b;
}
}
package inheritance;
import java.util.Date;
import java.util.GregorianCalendar
public 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 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;
}
}
public static void main(String[] args){
//constrcut a Manager object
Manager boss = new Manager("Cracker",80000,1988,12,15); boss.setBonus(5000);
Employee[] staff = new Employee[3]; //fill the staff arry with Manager and //Employee object
staff[0] = boss;
staff[1] = new Employee("Harry",50000,1986,10,1);
staff[2] = new Employee("Tommy",40000,1987,3,15);
//print out information about all Employee objects
//体现了多态与动态捆绑
for(Employee e : staff)
System.out.println("name:" + e.getName() + ",salary:" + e.getSalary());
}
进行类型转换的唯一原因是:**在暂时忽视对象的实际类型之后,使用对象的全部功能。**例如,在Manager类中,由于某些项是普通雇员,所以staff数组必须是Employee对象的数组。我们需要将数组中引用经理的元**素复原成Manager类,**以便能够访问新增加的所有变量(需要注意,在前面的代码中,为了避免类型转换,我们做了**一些特别的处理**,即将boss变量存入数组之前,先用Manager对象对它进行初始化。为了设置经理的奖金,必须使用正确的类型)
1.什么叫暂时忽视对象的类型,使用对象的全部功能?
2.什么叫复原成Manager类,以便能够访问新增加的所有变量?这些新增加的变量是啥,能否指出来?
3.上段话还提出了做出了特别的处理,完全搞不懂在哪儿?