📄 testemployee.java
字号:
public class TestEmployee {
public static void main(String[] args) {
Employee[] es=new Employee[4];
es[0]=new SalariedEmployee("Huxz",8,8000);
es[1]=new HourlyEmployee("Liucy",2,20,300);
es[2]=new SalesEmployee("Zhangll",8,100000,0.05);
es[3]=new BasePlusSalesEmployee("Wanglin",8,10000,0.5,10000);
for(int i=0;i<es.length;i++){
System.out.println(es[i].getName()+":"+es[i].getSalary(8));
}
}
}
class Employee{
private String name;
private int birthMonth;
public Employee(String name,int birthMonth){
this.name=name;
this.birthMonth=birthMonth;
}
public String getName(){
return name;
}
public double getSalary(int month){
if (this.birthMonth==month) return 100;
else return 0;
}
}
class SalariedEmployee extends Employee{
private double salary;
public SalariedEmployee(String name,int birthMonth,double salary){
super(name,birthMonth);
this.salary=salary;
}
public double getSalary(int month){
return salary+super.getSalary(month);
}
}
class HourlyEmployee extends Employee{
private double salaryPerHour;
private int hours;
public HourlyEmployee(String name, int birthMonth, double salaryPerHour, int hours) {
super(name, birthMonth);
this.salaryPerHour = salaryPerHour;
this.hours = hours;
}
public double getSalary(int month){
double result=0;
if (hours>160) result=160*this.salaryPerHour+(hours-160)*this.salaryPerHour*1.5;
else result=this.hours*this.salaryPerHour;
return result+super.getSalary(month);
}
}
class SalesEmployee extends Employee{
private double sales;
private double rate;
public SalesEmployee(String name, int birthMonth, double sales, double rate) {
super(name, birthMonth);
this.sales = sales;
this.rate = rate;
}
public double getSalary(int month) {
return this.sales*this.rate+super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee{
private double basedSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double sales, double rate, double basedSalary) {
super(name, birthMonth, sales, rate);
this.basedSalary = basedSalary;
}
public double getSalary(int month) {
return this.basedSalary+super.getSalary(month);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -