子类与继承:实验3

抽象类:

package Employee_;

public abstract class Employee {
	public abstract double earnings(double money);
}

子类:

package Employee_;

public class YearWorker extends Employee{

	@Override
	public double earnings(double day_money) {
		// TODO Auto-generated method stub
		return day_money*30*12*2;
	}

}


package Employee_;

public class WeekWorker extends Employee{

	@Override
	public double earnings(double day_money) {
		// TODO Auto-generated method stub
		return (day_money+100)*7;
	}
}


package Employee_;

public class MonthWorker extends Employee{
	@Override
	public double earnings(double day_money) {
		// TODO Auto-generated method stub
		return day_money*30;
	}
}


类:

/**
 * 
 */

/**
 * @author Administrator
 *
 */
import Employee_.*;
public class Company {
	Employee a[];
	double salariespay=0;
	
	public double salariesPay(Employee[] guy,double day_money) {
		a=guy;
		for(int i=0; i<a.length; i++) {
			salariespay+=a[i].earnings(day_money);
		}
		return salariespay;
	}
}

主类:

import Employee_.*;

public class CompanySalary {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Employee guy[]=new Employee[29];
		for(int i=0; i<guy.length; i++) {
			if(i%3==0) guy[i]=new WeekWorker();
			else if(i%3==1) guy[i]=new MonthWorker();
			else guy[i]=new YearWorker();
		}
		Company a=new Company();
		System.out.println("公司薪水总额:"+a.salariesPay(guy, 780)+'元');
	}

} 



posted on 2017-11-18 17:17  MACHINE_001  阅读(158)  评论(0编辑  收藏  举报

导航