某公司的雇员分为以下若干类 Employee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 SalariedEmployee :Employee 的子类,拿固定工资的员工。属性:月薪 HourlyEmployee :Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数 SalesEmployee :Employee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 定义一个类Company,在main方法,把若干各种类型的员工放在一个Employee 数组里,并单元出数组中每个员工当月的工资。
时间: 2023-06-23 13:08:05 浏览: 263
以下是代码实现:
```java
import java.util.Scanner;
public class Company {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("Tom", 10000);
employees[1] = new HourlyEmployee("Jerry", 100, 180);
employees[2] = new SalesEmployee("Bob", 5000, 0.05);
employees[3] = new Employee("Alice", 7);
for (Employee employee : employees) {
System.out.println(employee.getName() + "'s salary of this month is " + employee.getSalary(7));
}
}
}
class Employee {
private String name;
private int birthMonth;
Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
String getName() {
return name;
}
int getBirthMonth() {
return birthMonth;
}
double getSalary(int month) {
double salary = 0.0;
if (month == birthMonth) {
salary += 100;
}
return salary;
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
SalariedEmployee(String name, double monthlySalary) {
super(name, 0);
this.monthlySalary = monthlySalary;
}
@Override
double getSalary(int month) {
double salary = super.getSalary(month);
return salary + monthlySalary;
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private int monthlyWorkHours;
HourlyEmployee(String name, double hourlyWage, int monthlyWorkHours) {
super(name, 0);
this.hourlyWage = hourlyWage;
this.monthlyWorkHours = monthlyWorkHours;
}
@Override
double getSalary(int month) {
double salary = super.getSalary(month);
if (monthlyWorkHours > 160) {
salary += (monthlyWorkHours - 160) * hourlyWage * 1.5 + 160 * hourlyWage;
} else {
salary += monthlyWorkHours * hourlyWage;
}
return salary;
}
}
class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
SalesEmployee(String name, double monthlySales, double commissionRate) {
super(name, 0);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
@Override
double getSalary(int month) {
double salary = super.getSalary(month);
salary += monthlySales * commissionRate;
return salary;
}
}
```
在本例中,定义了一个父类Employee和三个子类SalariedEmployee、HourlyEmployee和SalesEmployee。每个子类继承了父类的属性和方法,并且根据不同的员工类型,重写了getSalary方法来计算每个员工的工资。主函数中,定义了一个Employee数组,分别放置了不同类型的员工,并且打印出了每个员工当月的工资。
阅读全文