java题库

更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
某公司的员工分为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)每个类都完全封装,不允许有非私有化属性。 // 父类员工 abstract 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 abstract double getSalary(int month); protected double addBirthBonus(int month, double s) { return birthMonth == month ? s + 100 : s; } } // 月薪员工 class SalariedEmployee extends Employee { private double monthPay; public SalariedEmployee(String n, int b, double m) {super(n,b);monthPay=m;} @Override public double getSalary(int m) { return addBirthBonus(m, monthPay); } } // 计时员工 class HourlyEmployee extends Employee { private double hourWage; private int hour; public HourlyEmployee(String n, int b, double w, int h) {super(n,b);hourWage=w;hour=h;} @Override public double getSalary(int m) { double s = hour <= 160 ? hour*hourWage : 160*hourWage + (hour-160)*hourWage*1.5; return addBirthBonus(m, s); } } // 无底薪销售 class SalesEmployee extends Employee { private double sale, rate; public SalesEmployee(String n, int b, double s, double r) {super(n,b);sale=s;rate=r;} @Override public double getSalary(int m) { return addBirthBonus(m, sale*rate); } } // 底薪销售 class BasePlusSalesEmployee extends SalesEmployee { private double base; public BasePlusSalesEmployee(String n, int b, double s, double r, double ba) {super(n,b,s,r);base=ba;} @Override public double getSalary(int m) { return addBirthBonus(m, base + super.getSalary(m))-addBirthBonus(m,0); } } // 测试类 public class Test { public static void main(String[] args) { Employee[] emps = { new SalariedEmployee("张三",5,6000), new HourlyEmployee("李四",3,40,180), new SalesEmployee("王五",8,20000,0.1), new BasePlusSalesEmployee("赵六",5,30000,0.08,2000) }; int month = 5; System.out.println("=== "+month+"月工资 ==="); for(Employee e : emps) System.out.println(e.getName()+":"+e.getSalary(month)); } }