某公司的员工分为5类,每类员工都有相应的封装类。(黑马第三版Java基础入门 第四章编程题)

您所在的位置:网站首页 java基础入门题目 某公司的员工分为5类,每类员工都有相应的封装类。(黑马第三版Java基础入门 第四章编程题)

某公司的员工分为5类,每类员工都有相应的封装类。(黑马第三版Java基础入门 第四章编程题)

2024-07-12 02:25| 来源: 网络整理| 查看: 265

(1) Employee:这是所有员工总的父类。

① 属性:员工的姓名,员工的生日月份

② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励 100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5倍工资发放。

① 属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。

① 属性:月销售额、提成率。

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。

① 属性:底薪。

本题要求根据上述雇员分类,编写一个程序,实现以下功能:

(1)创建一个 Employee 数组,分别创建若干不同的 Employee 对象,并打印某个月的工资。

(2)每个类都完全封装,不允许非私有化属性

代码如下:

父类:

abstract class Employee {     private String name;     private int month;

    public Employee(String name, int month) {         this.name = name;         this.month = month;     }

    public String getName() {         return name;     }

    public int getMonth() {         return month;     }

    public void setName(String name) {         this.name = name;     }

    public void setMonth(int month) {         this.month = month;     }

    public double getSalary(int month) {         double salary = 0;         if (this.month == month) {             salary += 100;         }         return salary;     } }固定工资的员工: class SalariedEmployee extends Employee  {     private double monthsalary;//月薪     public SalariedEmployee (String name,int month,double monthsalary){         super(name,month);         this.monthsalary =monthsalary ;     }

    public double getMonthsalary() {         return monthsalary;     }

    public void setMonthsalary(double monthsalary) {         this.monthsalary = monthsalary;     }

    public double getSalary(int month){        return monthsalary+super.getSalary(month);     } }

按小时拿工资的员工:

public class HourlyEmployee extends Employee { private double hourlysalary;//每小时工资 private double monthhourly;//每月小时数 public HourlyEmployee (String name,int month,double monthhourly ,double hourlysalary ){ super(name,month); this.hourlysalary =hourlysalary ; this.monthhourly =monthhourly ; } public double getHourlysalary() { return hourlysalary; } public void setHourlySalary(double hourlysalary) { this.hourlysalary = hourlysalary; } public double getMonthhourly() { return monthhourly; } public void setMonthhourly(double monthhourly) { this.monthhourly = monthhourly; } public double getSalary(int month) { double salary = 0; if (monthhourly > 160) { salary = (160 + (monthhourly - 160) * 3/2) * hourlysalary ; } else { salary = month * hourlysalary ; } return salary + super.getSalary(month); } }

销售人员:

public class SalesEmployee extends Employee { private double salasmonth;//月销售额 private double commmissionrate;//提成率 public SalesEmployee (String name,int month,double salasmonth ,double commmissionrate ){ super(name,month); this.commmissionrate =commmissionrate ; this.salasmonth =salasmonth ; } public double getSalasmonth() { return salasmonth; } public double getCommmissionrate() { return commmissionrate; } public void setSalasmonth(double salasmonth) { this.salasmonth = salasmonth; } public void setCommmissionrate(double commmissionrate) { this.commmissionrate = commmissionrate; } @Override public double getSalary(int month) { return commmissionrate *salasmonth +super.getSalary(month); } }

固定底薪的销售员(销售人员的子类):

public class BasePlusSalesEmployee extends SalesEmployee { private double basesalary;//底薪 public BasePlusSalesEmployee (String name,int month,double basesalary,double salasmonth ,double commmissionrate ){ super(name,month,salasmonth ,commmissionrate ); this.basesalary =basesalary ; } public double getBasesalary() { return basesalary; } public void setBasesalary(double basesalary) { this.basesalary = basesalary; } @Override public double getSalary(int month) { return basesalary +super .getSalary(month); } }

测试类:

public class Texe { public static void main(String[] args) { Employee stu[]={ new SalariedEmployee("李四",7,5000), new HourlyEmployee("张三",8,20,260), new SalesEmployee("王二",7,8000,0.2), new BasePlusSalesEmployee("赵六",9,2000,6000,0.1) }; System .out .println("七月份工资:"); for(int i=0;i< stu.length ;i++){ System .out.println("姓名:"+stu[i].getName()+" 生日月份:"+stu[i].getMonth()+" 工资为:"+ stu[i].getSalary(7)); } }

调试结果图:

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3