案例介绍:
某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下。
(1)Employee:这是所有员工的父类。
①属性:员工的姓名、员工的生日月份。
②方法:getSalary(int month)根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。
(2)SalariedEmployee:Employee的子类,拿固定工资的员工。
属性:月薪。
(3)HourlyEmployee:Employee的子类,按小时拿工资的员工,每月超出160h的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数。
(4)SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额,提成率。
(5)BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为底薪加上销售提成。
属性:底薪。
要求根据上述员工分类,编写一个程序,实现以下功能:
(1)创建一个Employee数组,分别创建若干不同的Employee对象,并打印某个月的工资。
(2)每个类都完全封装,不允许有非私有化属性。
运行结果:
//Employee:这是所有员工的父类
class Employee {
private String name; //员工姓名
private int monthOfBirthday; //生日月份
public Employee() {} //无参构造函数
public Employee(String name,int monthOfBirthday) {
this.name=name;
this.monthOfBirthday=monthOfBirthday;
}
//计算工资,参数month是月份,如果当月是员工生日,奖励100元
public double getSalary(int month) {
return (this.monthOfBirthday==month)?100:0;
}
public void setName(String name) {
this.name=name;
}
public void setMonthOfBirthday(int monthOfBirthday) {
this.monthOfBirthday = monthOfBirthday;
}
public String getName() {
return name;
}
public int getMonthOfBirthday() {
return monthOfBirthday;
}
}
//SalariedEmployee:Employee的子类,拿固定工资的员工
class SalariedEmployee extends Employee {
private double monthSalary; //月薪
public SalariedEmployee() {} //无参构造函数
public SalariedEmployee(String name,int monthOfBirthday,double monthSalary) {
super(name, monthOfBirthday); //通过super关键字调用Employee类有两个参数的构造方法
this.monthSalary=monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary=monthSalary;
}
public double getMonthSalary() {
return monthSalary;
}
@Override
public double getSalary(int month) {
return monthSalary + super.getSalary(month);
}
}
//HourlyEmployee:Employee的子类,按小时拿工资的员工,每月超出160h的部分按照1.5倍工资发放
class HourlyEmployee extends Employee {
private double hourlyWage; //每小时工资
private int workHours; //当月工作小时数
public HourlyEmployee() {} //无参构造函数
public HourlyEmployee(String name,int monthOfBirthday,double hourlyWage,int workHours) {
super(name, monthOfBirthday); //通过super关键字调用Employee类有两个参数的构造方法
this.hourlyWage=hourlyWage;
this.workHours=workHours;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public void setWorkHours(int workHours) {
this.workHours = workHours;
}
public double getHourlyWage() {
return hourlyWage;
}
public int getWorkHours() {
return workHours;
}
@Override
public double getSalary(int month) {
if(workHours<0){ // 如果工作小时数小于0,输出数据错误
System.out.println("数据错误,工作小时数小于0");
return 0;
}
else if(workHours<=160) { //小于160小时
return workHours*hourlyWage + super.getSalary(month);
}
else { //超出160个小时的工作时长,按照1.5倍计算
return 160*hourlyWage + (workHours-160)*hourlyWage*1.5 + super.getSalary(month);
}
}
}
//SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定
class SalesEmployee extends Employee {
private double sales; //销售额
private double rate; //提成率
public SalesEmployee() {} //无参构造函数
public SalesEmployee(String name,int monthOfBirthday,double sales,double rate) {
super(name,monthOfBirthday);
this.sales=sales;
this.rate=rate;
}
public void setSales() {
this.sales=sales;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getSales() {
return sales;
}
public double getRate() {
return rate;
}
@Override
public double getSalary(int month) {
return sales*rate + super.getSalary(month);
}
}
//BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为底薪加上销售提成
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary; //底薪
public BasePlusSalesEmployee() {} //无参构造函数
public BasePlusSalesEmployee(String name,int monthOfBirthday,double sales,double rate,double baseSalary) {
super(name,monthOfBirthday,sales,rate);
this.baseSalary=baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double getSalary(int month) {
return baseSalary + super.getSalary(month);
}
}
//测试类
public class Test {
public static void main(String[] args){
//声明一个Employee类型的数组,并创建不同子类型的对象
Employee[] employee = {
new SalariedEmployee("张三",1,6000),//张三,1月生日,月薪6000
new HourlyEmployee("李四",2,50,180),//李四,2月生日,工作180小时,时薪50
new SalesEmployee("王五",3,50000,0.15),//王五,3月生日,销售额50000,提成率0.15
new BasePlusSalesEmployee("赵六",4,40000,0.15,2000),//赵六,4月生日,,销售额40000,提成率0.15,底薪2000
};
//打印每个员工的工资
for(Employee emp:employee) {
System.out.println(emp.getName()+" 10月工资 "+Math.round(emp.getSalary(10)));
}
}
}
完整代码:
class Employee {
private String name; //员工姓名
private int monthOfBirthday; //生日月份
public Employee() {} //无参构造函数
public Employee(String name,int monthOfBirthday) {
this.name=name;
this.monthOfBirthday=monthOfBirthday;
}
//计算工资,参数month是月份,如果当月是员工生日,奖励100元
public double getSalary(int month) {
return (this.monthOfBirthday==month)?100:0;
}
public void setName(String name) {
this.name=name;
}
public void setMonthOfBirthday(int monthOfBirthday) {
this.monthOfBirthday = monthOfBirthday;
}
public String getName() {
return name;
}
public int getMonthOfBirthday() {
return monthOfBirthday;
}
}
class SalariedEmployee extends Employee {
private double monthSalary; //月薪
public SalariedEmployee() {} //无参构造函数
public SalariedEmployee(String name,int monthOfBirthday,double monthSalary) {
super(name, monthOfBirthday); //通过super关键字调用Employee类有两个参数的构造方法
this.monthSalary=monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary=monthSalary;
}
public double getMonthSalary() {
return monthSalary;
}
@Override
public double getSalary(int month) {
return monthSalary + super.getSalary(month);
}
}
class HourlyEmployee extends Employee {
private double hourlyWage; //每小时工资
private int workHours; //当月工作小时数
public HourlyEmployee() {} //无参构造函数
public HourlyEmployee(String name,int monthOfBirthday,double hourlyWage,int workHours) {
super(name, monthOfBirthday); //通过super关键字调用Employee类有两个参数的构造方法
this.hourlyWage=hourlyWage;
this.workHours=workHours;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public void setWorkHours(int workHours) {
this.workHours = workHours;
}
public double getHourlyWage() {
return hourlyWage;
}
public int getWorkHours() {
return workHours;
}
@Override
public double getSalary(int month) {
if(workHours<0){ // 如果工作小时数小于0,输出数据错误
System.out.println("数据错误,工作小时数小于0");
return 0;
}
else if(workHours<=160) { //小于160小时
return workHours*hourlyWage + super.getSalary(month);
}
else { //超出160个小时的工作时长,按照1.5倍计算
return 160*hourlyWage + (workHours-160)*hourlyWage*1.5 + super.getSalary(month);
}
}
}
class SalesEmployee extends Employee {
private double sales; //销售额
private double rate; //提成率
public SalesEmployee() {} //无参构造函数
public SalesEmployee(String name,int monthOfBirthday,double sales,double rate) {
super(name,monthOfBirthday);
this.sales=sales;
this.rate=rate;
}
public void setSales() {
this.sales=sales;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getSales() {
return sales;
}
public double getRate() {
return rate;
}
@Override
public double getSalary(int month) {
return sales*rate + super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary; //底薪
public BasePlusSalesEmployee() {} //无参构造函数
public BasePlusSalesEmployee(String name,int monthOfBirthday,double sales,double rate,double baseSalary) {
super(name,monthOfBirthday,sales,rate);
this.baseSalary=baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double getSalary(int month) {
return baseSalary + super.getSalary(month);
}
}
public class Test {
public static void main(String[] args){
//声明一个Employee类型的数组,并创建不同子类型的对象
Employee[] employee = {
new SalariedEmployee("张三",1,6000),//张三,1月生日,月薪6000
new HourlyEmployee("李四",2,50,180),//李四,2月生日,工作180小时,时薪50
new SalesEmployee("王五",3,50000,0.15),//王五,3月生日,销售额50000,提成率0.15
new BasePlusSalesEmployee("赵六",4,40000,0.15,2000),//赵六,4月生日,,销售额40000,提成率0.15,底薪2000
};
//打印每个员工的工资
for(Employee emp:employee) {
System.out.println(emp.getName()+" 10月工资 "+Math.round(emp.getSalary(10)));
}
}
}