目录
1.1任务描述
某公司的员工分为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)每个类都完全封装,不允许非私有化属性。
1.2任务目标
(1)学会分析“员工属性及工资分析”程序任务实现的逻辑思路。
(2)能够独立完成“员工属性及工资分析”程序的源代码编写、编译及运行。
(3)理解和掌握面向对象的设计过程。
(4)掌握类的结构和定义过程。
(5)掌握构造方法及其重载。
(6)掌握对象的创建和使用。
1.3任务实现思路
(1)通过任务描述可知,需要定义一个员工类Employee实现员工每月可获得的薪酬。
(2)先定义父类Employee,再定义其子类SalariedEmployee,HourlyEmployee,SalesEmployee(其下又定义子类BasePlusSalesEmployee),实现5类员工相应的封装类。
(3)创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。
1.4任务实现源码
abstract class Employee{ private String name; //定义姓名name并私有化属性 private int month; //定义生日月份month并私有化属性 public Employee(){} //无参构造器 public Employee(String name,int month){ //有参构造方法 this.name = name; //给属性name初始化赋值 this.month = month; //给属性month初始化赋值 } //获取属性name的方法 public String getName(){ return name; //返回name属性 } //获取属性month的方法 public int getMonth(){ return month; //返回month属性 } //给属性name赋初始值 public void setName(String name){ this.name = name; //本类中的属性name } //给属性month赋初始值 public void setMonth(int month){ this.month = month; //本类中的属性month } //创建一个方法getSalary()用来计算工资,参数month是月份,如果当月是员工生日,奖 励100元 public double getSalary(int month){ double salary = 0; //定义工资变量 //判断当前月份是否是员工的生日月份,如果是奖励100元 if(this.month == month){ salary = salary + 100; } return salary; //返回工资salary } } class SalariedEmployee extends Employee{ private double monthSalary; //封装monthSalary属性 public SalariedEmployee(){} //无参构造方法 //有参构造方法 参数 姓名 生日月份 月薪 public SalariedEmployee(String name,int month,double monthSalary){ super(name,month); //调用父类有参构造方法 this.monthSalary = monthSalary; //为属性monthSalary初始化赋值 } //获取monthSalary的值 public double getMonthSalary(){ return monthSalary; } //给monthSalary赋值 public void setMonthSalary(double monthSalary){ this.monthSalary = monthSalary; } //覆盖父类中的方法 public double getSalary(int month){ double salary = monthSalary+super.getSalary(month); //定义工资变量 return salary; } } class HourlyEmployee extends Employee{ private double hourlySalary; //定义属性hourlySalary每小时的工资 private int hours; //定义属性hours每月工作的小时数 public HourlyEmployee(){} //无参构造方法 //有参构造方法 参数 姓名 生日月份 每小时的工资 每月工作的小时数 public HourlyEmployee(String name,int month,double hourlySalary,int hours){ super(name,month); //调用父类有参构造方法 this.hourlySalary = hourlySalary ; //为属性hourlySalary初始化赋值 this.hours = hours; //为属性hours 初始化赋值 } public double getHourlySalary(){ //获取hourlySalary的值 return hourlySalary; } public int getHours(){ //获取hours的值 return hours; } //定义set方法设置hourlySalary hours的值 public void setHourlySalary(double hourlySalary){ this.hourlySalary =hourlySalary; } public void setHourly(int hours){ this.hours = hours; } //覆盖父类方法 public double getSalary(int month){ if(hours < 0){ //如果工作小时数小于0 输出数据错误 System.out.println("数据错误"); return 0; } //小于160个小时的 按照每个月的工作小时数乘以每小时的工资 else if(hours <= 160) return hourlySalary*hours+super.getSalary(month); //超出160个小时的小时数 按照1.5倍计算 else return hourlySalary*160+hourlySalary*1.5*(hours-160)+super.getSalary(month); } } class SalesEmployee extends Employee{ private double sales ; //定义销售额sales private double rate; //定义提成率rate public SalesEmployee(){} public SalesEmployee(String name,int month,double sales,double rate){ super(name,month); this.sales = sales; this.rate = rate; } public double getSales(){ return sales; } public double getRate(){ return rate; } public void setSales(double sales){ this.sales = sales; } public void setRate(double rate){ this.rate = rate; } public double getSalary(int month){ return this.getSales()*(1+this.getRate())+super.getSalary(month); } } class BasePlusSalesEmployee extends SalesEmployee{ private double baseSalary; //定义基础工资baseSalary //无参构造方法 public BasePlusSalesEmployee(){} //有参构造方法 public BasePlusSalesEmployee(String name,int month,double sales,double rate,double baseSalary){ super(name,month,sales,rate); this.baseSalary = baseSalary; } //get/set方法对私有属性的调用和设置 public double gatBaseSalary(){ return baseSalary; } public void setBaseSalary(){ this.baseSalary = baseSalary; } 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), new HourlyEmployee("李四",2,50,180), new SalesEmployee("王五",3,6500,0.15), new BasePlusSalesEmployee("赵六",4,5000,0.15,2000)}; //打印每个员工的工资 for(int i = 0; i < employee.length ;i++) System.out.println(Math.round(employee[i].getSalary(10))); } }