java实现 *Cola公司的雇员分为以下若干类: (1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 (2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。 属性:月薪 (3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。 属性:每小时的工资、每月工作的小时数 (4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。 属性:月销售额、提成率 (5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。 */
时间: 2024-02-09 20:09:15 浏览: 67
java代码-20软三35号 定义一个员工信息类Employee,使用ArrayList对员工信息进行添加和显示。
好的,下面是具体的实现代码:
```java
// ColaEmployee.java
public class ColaEmployee {
protected String name;
protected int birthMonth;
public ColaEmployee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public double getSalary(int month) {
double salary = 0;
return salary;
}
}
// SalariedEmployee.java
public class SalariedEmployee extends ColaEmployee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
@Override
public double getSalary(int month) {
double salary = monthlySalary;
if (month == birthMonth) {
salary += 100;
}
return salary;
}
}
// HourlyEmployee.java
public class HourlyEmployee extends ColaEmployee {
private double hourlySalary;
private int workingHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int workingHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.workingHours = workingHours;
}
@Override
public double getSalary(int month) {
double salary = hourlySalary * workingHours;
if (workingHours > 160) {
salary += hourlySalary * 1.5 * (workingHours - 160);
}
if (month == birthMonth) {
salary += 100;
}
return salary;
}
}
// SalesEmployee.java
public class SalesEmployee extends ColaEmployee {
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;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == birthMonth) {
salary += 100;
}
return salary;
}
}
// Company.java
public class Company {
public static void printSalary(ColaEmployee employee, int month) {
System.out.println(employee.name + "在" + month + "月的工资为:" + employee.getSalary(month));
}
}
// TestCompany.java
public class TestCompany {
public static void main(String[] args) {
ColaEmployee[] employees = new ColaEmployee[4];
employees[0] = new SalariedEmployee("Tom", 3, 3000);
employees[1] = new HourlyEmployee("Jerry", 4, 20, 180);
employees[2] = new SalesEmployee("John", 5, 50000, 0.05);
employees[3] = new SalariedEmployee("Mary", 6, 3500);
for (int i = 1; i <= 12; i++) {
System.out.println("==========" + i + "月份工资==========");
for (ColaEmployee employee : employees) {
Company.printSalary(employee, i);
}
}
}
}
```
在这个程序中,我们先定义了一个父类`ColaEmployee`,包含所有员工都有的属性和方法,然后分别定义了子类`SalariedEmployee`、`HourlyEmployee`和`SalesEmployee`,分别实现了各自的计算工资方法。最后定义了一个`Company`类,包含了一个静态方法`printSalary`,用来打印某个员工在某个月的工资。在`TestCompany`类中,我们实例化了不同类型的员工,并使用一个数组存储起来,然后循环输出每个员工在每个月的工资情况。
阅读全文