某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
写一个程序,把若干各种类型的员工放在一个Employee数组里,写一个方法,打印出某月每个员工的工资数额。
public class Topic1 {
public static void main(String[] args) {
SalariedEmployee sem = new SalariedEmployee();
sem.birthday =12;
sem.salary = 10000;
// System.out.println(sem.getSalary(12));
HourlyEmployee hem =new HourlyEmployee();
hem.birthday = 12;
hem.hours = 200;
hem.salaryHour = 30 ;
// System.out.println(hem.getSalary(12));
SalesEmployee sm = new SalesEmployee();
sm.birthday =12;
sm.total =800000 ;
sm.rate = 0.01;
// System.out.println(sm.getSalary(12));
BasePlusSalesEmployee bps =new BasePlusSalesEmployee();
bps.birthday = 12;
bps.total =800000;
bps.rate =0.01;
bps.baseSalary = 1500;
// System.out.println(bps.getSalary(12));
// 声明一个数组 数据类型 Employee类型 格式 : 数据类型[] 数组名称
/*Employee[] ee = new Employee[4];
ee[0]=sem;*/
Employee[] ee = {sem,hem,sm,bps};
//计算工资
for (int i = 0; i < ee.length; i++) {
System.out.println(ee[i].getSalary(12));
}
}
}
class Employee {
String name;
int birthday;
public double getSalary(int month) {
return birthday == month ? 100 : 0;
}
}
class SalariedEmployee extends Employee{
double salary;
public double getSalary(int month) {
return super.getSalary(month) + salary;
}
}
class HourlyEmployee extends Employee{
// 属性:每小时的工资、每月工作的小时数
double salaryHour;//每小时的工资
int hours;//小时数
public double getSalary(int month) {
return super.getSalary(month) +( hours-160 > 0?((hours-160)*1.5*salaryHour + 160*salaryHour) :(salaryHour* hours)) ;
}
}
class SalesEmployee extends Employee{
// 属性:月销售额、提成率
double total;
double rate;
@Override
public double getSalary(int month) {
// TODO Auto-generated method stub
return super.getSalary(month)+total* rate;
}
}
class BasePlusSalesEmployee extends SalesEmployee{
// 属性:底薪。
double baseSalary;
@Override
public double getSalary(int month) {
// TODO Auto-generated method stub
return super.getSalary(month)+ baseSalary ;
}
}