有一个abstract类,类名为Employee

要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年薪领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:Public abstract earnings();子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。

package homework;
/*
File name:Company.cpp
Author:杨柳
Date:2017/11/6
IDE:eclipse
*/
class Company {
	Employee[] em; //Employee作为成员
    Company(Employee[] em) { //构造函数
    	this.em=em;	
    } 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Employee[] em=new Employee[3];
		em[0]=new YearWorker(1,12000.0); //上转型对象
		em[1]=new MonthWorker(11,8000.0);
		em[2]=new WeekWorker(40,1200.0);
		Company com=new Company(em);
		double sum;
		sum=em[0].earnings()+em[1].earnings()+em[2].earnings();
		System.out.println("总工资为:"+sum);

	}

}
abstract class Employee {
	abstract double earnings(); //抽象方法
}
class MonthWorker extends Employee {
	int month;
	double monthSalary;
	MonthWorker(int m,double s){
		month=m;
		monthSalary=s;
	}
    double earnings() {
		return month*monthSalary;
	}

}
class WeekWorker extends Employee {
	int week;
	double weekSalary;
	WeekWorker(int w,double s){
		week=w;
		weekSalary=s;
	}
    double earnings() {
		return week*weekSalary;
	}
}
class YearWorker extends Employee {
	int year;
	double yearSalary;
	YearWorker(int y,double s){ //工作年数,年工资
		year=y;
		yearSalary=s;
	}
    double earnings() {  //重写抽象方法
		return year*yearSalary;
	}

}

 

 

好的,以下是一个实现上述需求的Java代码: ```java abstract class Employee { public abstract double earnings(); } class YearWorker extends Employee { private double salary; public YearWorker(double salary) { this.salary = salary; } @Override public double earnings() { return salary; } } class MonthWorker extends Employee { private double salary; public MonthWorker(double salary) { this.salary = salary; } @Override public double earnings() { return salary * 12; } } class WeekWorker extends Employee { private double salary; public WeekWorker(double salary) { this.salary = salary; } @Override public double earnings() { return salary * 52; } } class Company { private Employee[] employees; public Company(Employee[] employees) { this.employees = employees; } public double totalPay() { double total = 0; for (Employee e : employees) { total += e.earnings(); } return total; } } public class Main { public static void main(String[] args) { Employee[] employees = new Employee[3]; employees[0] = new YearWorker(50000); employees[1] = new MonthWorker(4000); employees[2] = new WeekWorker(800); Company company = new Company(employees); System.out.println("The company needs to pay " + company.totalPay() + " per year."); } } ``` 在这个程序中,Employee是抽象,定义了一个抽象方法earnings()。YearWorker、MonthWorker和WeekWorkerEmployee的子,分别实现了earnings()方法来计算各自领取薪水的方式。 最后,CompanyEmployee对象数据成员,Employee对象数组的单元可以是YearWorker对象的上转型对象或MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值