–
编程:某公司的雇员分为以下若干类:
(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会
额外奖励100 元。
(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。
(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分
按照1.5 倍工资发放。
① 属性:每小时的工资、每月工作的小时数。
(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
① 属性:月销售额、提成率。
(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加
上销售提成部分。
① 属性:底薪。
要求:
创建一个Employee 数组,分别创建若干不同的Employee对象,并打
印某个月的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。
类图如下:

package day7;
// 创建一个父类 Employee
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; //定义工资变量
if(this.month == month)salary = salary + 100; //判断当前月份是否是员工的生日月份,如果是奖励100元
return salary; //返回工资salary
}
}
//创建一个SalariedEmployee类,继承于Employee
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; //返回工资salary
}
}
//定义一个HourlyEmployee类,继承于Employee
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);
}
}
//声明一个BasePlusSalesEmployee 继承于SalesEmployee
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 Day7Exc27{
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)));
}
}
957



被折叠的 条评论
为什么被折叠?



