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;
} http://ask.csdn.net/questions/648777#
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 boss = (Manager) staff[1];// Error
行这个程序的时候,JAVA 运行时系统将报告这个错误,并产生一个ClassCastException异常。如果没有捕获这个异常,那么程序就会终止。因此应该养成一个良好的程序设计习惯: 在进行类型转换之前先查看一下是否能够成功的转换。这个过程简单地使用instanceof 运算符就可以实现。例如:
if(staff[1] instanceof Manager)
{
boss =(Manager)staff [1];
...
}
我有如下几个问题:
1. 强制性类型转换就是为了方便staff[1] 来调用子类中的某些方法,为什么这里会出现这么个判断,staff[1]本来就属于Manager啊,这个判断铁定是错的啊。
2.上面这个一段话中提到一个捕获异常,都出现异常了,程序肯定会终止,并且出现问题啊,为什么会说如果没有捕获这个异常,那么程序就会终止呢? 没有办法理解。