package home_work_4_17; abstract class Employee{ abstract double earnings(); } class YearWorker extends Employee{ double earnings(){ System.out.println("按年领取1000"); return 1000; } } class MonthWorker extends Employee{ double earnings(){ System.out.println("按月领取100"); return 100; } } class WeekWorker extends Employee{ double earnings(){ System.out.println("按星期领取10"); return 10; } } class Company{ int n; //该公司的人数 public Company(int n) { this.n=n;// TODO Auto-generated constructor stub } Employee E[]=new Employee[n]; double cal(){ //返回薪水综合 double sum=0; for(int j=0;j<n;j++){ sum+=E[j].earnings(); } return sum; } } public class work_2 { public static void main(String args[]) { Company c=new Company(3); c.E[0]=new WeekWorker(); c.E[1]=new MonthWorker(); c.E[2]=new YearWorker(); System.out.println("总支出:"+c.cal()); } }
原文:http://www.cnblogs.com/inione/p/6731254.html