用java编写以下程序某公司的雇员分为以下若干类: (1)Employee:这是所有员工总的父类。 ①属性:员工的姓名,员工的生日月份 ②方法:getSalary (int montb) 根据参数月份爽确定工资,如果该月员工过生日,则公司会额外奖励100元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ①属性:月薪。 (③HourlyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ①厲性:每小时的工资、每月工作的小时数。 (④) Sales Employee: Employee 的子类,销售:二资由月销售额和提成率决定。 ①厲性:月销售额、提成率。 (5) BasePlus Sales Employee: SalesEmployee 的子类,有間定底新的销售人员,工资由底薪加上销售提成部分。 ◎属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2023-06-26 13:10:15 浏览: 706
Java封装、继承、多态、super杂糅在一起的小练习,公司雇员员工工资问题(Employee),练习扩展:创建一个Employee数组,分别创建不同的Employee对象,并打印某个月的工资
5星 · 资源好评率100%
以下是Java程序代码实现:
```java
public class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public double getSalary(int month) {
double salary = 0;
// 根据月份确定工资
// 具体实现略
return salary;
}
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
}
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
@Override
public double getSalary(int month) {
double salary = monthlySalary;
// 如果该月为生日月份,则额外奖励100元
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int workedHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int workedHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.workedHours = workedHours;
}
public double getHourlySalary() {
return hourlySalary;
}
public int getWorkedHours() {
return workedHours;
}
@Override
public double getSalary(int month) {
double salary = hourlySalary * workedHours;
// 如果工作时间超过160小时,则按照1.5倍工资发放
if (workedHours > 160) {
salary += hourlySalary * 0.5 * (workedHours - 160);
}
// 如果该月为生日月份,则额外奖励100元
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
super(name, birthMonth);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
public double getMonthlySales() {
return monthlySales;
}
public double getCommissionRate() {
return commissionRate;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
// 如果该月为生日月份,则额外奖励100元
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) {
super(name, birthMonth, monthlySales, commissionRate);
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double getSalary(int month) {
double salary = baseSalary + super.getSalary(month);
// 如果该月为生日月份,则额外奖励100元
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("张三", 6, 5000);
employees[1] = new HourlyEmployee("李四", 8, 80, 180);
employees[2] = new SalesEmployee("王五", 10, 100000, 0.1);
employees[3] = new BasePlusSalesEmployee("赵六", 12, 80000, 0.08, 5000);
int month = 10;
for (Employee employee : employees) {
System.out.println("员工姓名:" + employee.getName());
System.out.println("员工生日月份:" + employee.getBirthMonth());
System.out.println(month + "月份工资:" + employee.getSalary(month));
System.out.println();
}
}
}
```
输出结果为:
```
员工姓名:张三
员工生日月份:6
10月份工资:5000.0
员工姓名:李四
员工生日月份:8
10月份工资:14800.0
员工姓名:王五
员工生日月份:10
10月份工资:11000.0
员工姓名:赵六
员工生日月份:12
10月份工资:10000.0
```
阅读全文