public class Text17 {
public static void main(String[] args) {
Employee[] s = new Employee[4];
s[0] = new SalariedEmployee("Tom",4,4000.0);
s[1] = new HourlyEmployee("marry",5,90,50);
s[2] = new SalesEmployee("lisa",6,50000,0.4);
s[3] = new BasePlusSalesEmployee("susan",4,40000,0.3,1000);
for(int i = 0; i<s.length; i++){
System.out.println(s[i].getName()+"的工资为"+s[i].getSalary(5));
}//遍历打印出各位员工的工资
}
}
class Employee {//公司父类
private String name;//姓名
private int month;//生日月份
public Employee(){}
public Employee(String name, int month) {
super();
this.name = name;
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
//如果员工过生日,公司会额外奖励100元
public double getSalary(int month) {
if (month == this.month) {
return 100.0;
} else {
return 0.0;
}
}
}
//拿固定工资的员工
class SalariedEmployee extends Employee {
private double salary;//月薪
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public double getSalary(int month) {
double birthday = super.getSalary(month);
return salary + birthday;
}
}
//小时工,每月工作超出160个小时按照1.5倍工资算
class HourlyEmployee extends Employee {
private int hours;//每个月的小时数
private double hourlySalary;//每个小时的工资
public HourlyEmployee(String name, int month,int hours, double hourlySalary) {
super(name, month);
this.hours = hours;
this.hourlySalary = hourlySalary;
}
public double getSalary(int month) {
double birthday = super.getSalary(month);//判断是否有奖金
if (hours > 0 && hours <= 160) {//160个小时内的工资
return hours * hourlySalary + birthday;
} else {//160个小时外的工资
return 160 * hourlySalary + (hours - 160) * hourlySalary * 1.5 + birthday;
}
}
}
//销售员工
class SalesEmployee extends Employee {
private double sales;//月销售额
private double rate;//提成率
public SalesEmployee(String name, int month, double sales, double rate) {
super(name, month);
this.sales = sales;
this.rate = rate;
}
public double getSalary(int month) {
double birthday = super.getSalary(month);//判断是否有奖金
return sales * rate + birthday;
}
}
//有固定的底薪的销售人员,工资由底薪加销售提成
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary;//底薪
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double baseSalary) {
super(name, month, sales, rate);
this.baseSalary = baseSalary;
}
public double getSalary(int month) {
double birthday = super.getSalary(month);//引用其父类销售员工方法计算其提成和是否有生日金
return baseSalary + birthday;
}
}
多态 继承 练习题 计算某个月四种对象的工资
最新推荐文章于 2021-12-04 17:43:41 发布