abstract class Worker{
private String name;
private int age;
private double pay;
Worker(String name,int age,double pay){//2
this.name = name;
this.age = age;
this.pay = pay;
}
abstract void work();//该方法必须被重写
void sleep(){
}
}
class Manager extends Worker{
private int bonus;
Manager(String name,int age,double pay,int bonus){//3
super(name,age,pay);//这里改为这样。那是因子类继承了父类,而且父类已经做完了初始化,那么这样super(name,age,pay)就会调用2不这里的初始化。
this.bonus = bonus;
}
public void work(){
System.out.println("working"+bonus);
}
}
public class AbstractDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Manager m = new Manager("lisi",20,200.0,100);//1 这个会到3这里进行初始化。
}
} |