@Java
大家好,我是Ziph!
下面分为三种打印方法:
import java.util.Scanner;
/**
* @author Ziph
* @date 2020年2月21日
* @Email [email protected]
*/
public class TestDemo {
public static void main(String[] args) {
//-----------------------------------------------------------------------------------------
//方法一、
SalariedEmployee e1 = new SalariedEmployee("Ziph", 3, 100000);
HourlyEmployee e2 = new HourlyEmployee("Join", 4, 170, 20);
SalesEmployee e3 = new SalesEmployee("Marry", 8, 20000, 0.45);
BasePlusSalesEmployee e4 = new BasePlusSalesEmployee("Tom", 11, 30000, 0.45 , 5000);
System.out.println("该员工的工资为:" + e1.getSalary(3));
System.out.println("该员工的工资为:" + e2.getSalary(3));
System.out.println("该员工的工资为:" + e3.getSalary(3));
System.out.println("该员工的工资为:" + e4.getSalary(3));
//-----------------------------------------------------------------------------------------
//方法二、
//此方法避免了代码的重复,降低了耦合性,提高了计算机的执行效率。
Employee e1 = new SalariedEmployee("Ziph", 3, 100000);
Employee e2 = new HourlyEmployee("Join", 4, 170, 20);
Employee e3 = new SalesEmployee("Marry", 8, 20000, 0.45);
Employee e4 = new BasePlusSalesEmployee("Tom", 11, 30000, 0.45 , 5000);
TestDemo tt = new TestDemo();
tt.print(e1, 3);
tt.print(e2, 3);
tt.print(e3, 3);
tt.print(e4, 3);
//-----------------------------------------------------------------------------------------
//方法三、
//创建一个Employee数组,分别创建不同的Employee对象,并打印某个月的工资
Employee[] ee = new Employee[] {
new SalariedEmployee("Ziph", 3, 100000),
new HourlyEmployee("Join", 4, 170, 20),
new SalesEmployee("Marry", 8, 20000, 0.45),
new BasePlusSalesEmployee("Tom", 11, 30000, 0.45 , 5000)
};
for (int i = 0; i < ee.length; i++) {
print(ee[i], 3);
}
//-----------------------------------------------------------------------------------------
}
//打印工资
public void print(Employee e, int month) {
System.out.println("该员工的工资为:" + e.getSalary(month));
}
}
class Employee {//所有公司父类
private String name;//员工姓名
private int month;//员工的生日月份
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
//如果员工过生日,公司会额外奖励100元
public double getSalary(int month) {
if (month != this.month) {
return 0.0;
} else {
return 100.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 bonus = super.getSalary(month);//判断是否有奖金
return salary + bonus;
}
}
//小时工,每月工作超出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 bonus = super.getSalary(month);//判断是否有奖金
if (hours > 0 && hours <= 160) {//160个小时内的工资
return hours * hourlySalary + bonus;
} else {//160个小时外的工资
return 160 * hourlySalary + (hours - 160) * hourlySalary * 1.5 + bonus;
}
}
}
//销售员工,工资由月销售额和提成率决定
class SalesEmployee extends Employee {
private double sales;//月銷售額
private double rate;//提成率
public SalesEmployee(String name, int month) {
super(name, month);
}
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 bonus = super.getSalary(month);//判断是否有奖金
return sales * rate + bonus;
}
}
//有固定的底薪的销售人员,工资由底薪加销售提成
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 bonus = super.getSalary(month);//月销售乘提成率(是否有含奖金)
return baseSalary + bonus;
}
}
执行结果依次测试都是同一个答案:
有问题请大家留言回复!