abstract class Employee{
public abstract double earnings();
}
class YearWorker extends Employee{
public double earnings() {//重写earnings()方法 50000元
return 50000;
}
}
class MonthWorker extends Employee{
public double earnings(){//重写earnings()方法。2400元
return 2400;
}
}
class WeekWorker extends Employee{
public double earnings(){//重写earnings()方法。400元
return 400;
}
}
class Company{
Employee[] employee;//对象数组
double salaries=0;
Company(Employee[] employee){
this.employee=employee;
}
public double salariesPay(){
salaries=0;
for(int i=0;i<employee.length;i++){
salaries=salaries+employee[i].earnings(); //计算salaries。
}
return salaries;
}
}
public class HardWork{
public static void main(String args[]){
Employee[] employee=new Employee[20];//数组初始化
for(int i=0;i<employee.length;i++){
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
}
Company company=new Company(employee);//调用Company的构造方法
System.out.println("公司年工资总额:"+company.salariesPay());
}
}
内存执行情况:
刚学习j2se,希望和大家一起讨论学习,一起进步!