程序描述 按面向对象的理念,封装如下类型 员工类[Employee],拥有姓名、手机号、基本工资3个属性,并提供获取月薪的方法[getMonthSalary] 设计一个小时工类[HourlyWorker],他的基本工资为0, 拥有 每月工作小时数,每小时费用 2个属性,并重写 父类的 getMonthSalary方法 设计一个销售员工类[SalesWorker],拥有 提成率和月销售额 2个属性,并重写父类的 getMonthSalary方法 设计一个固定工资的员工类[FixWageWorker],无其它属性 要求: 所有类名必需首字母大写 所有属性名最好使用英文单词,不建议使用拼音,如果一定要写拼音,请写好注释 子类重写父类的方法时,在方法上面要打上 @Override 注解 额外要求1: 为了达到面向父类编程的目的,请开发出 EmployeeFactory工厂类,提供创建员工的静态方法以及根据 员工来获取当月工资,并打印输出。申明如下: 额外要求2: 再开发一个调用者类[UseEmployee],以数组的方式存放创建好的员工【要求每种员工对象都要有一 个】,并打印输出当月的工资。 public class EmployeeFactory { //方法一、创建员工对象的工厂方法 public static Employee createEmployee(/**TODO 参数请自己定义 */ ) { //TODO 请自行实现代码 } //方法二、根据员工来输出此员工的当月工资 public static void printMonthSalary(Employee e) { //TODO 请自行实现 } } public class UseEmployee { // public static void main(String[] args) { //TODO 请自行实现调用... }
时间: 2024-01-22 21:21:29 浏览: 118
Employee 类:
```java
public class Employee {
protected String name; //姓名
protected String phoneNumber; //手机号
protected double baseSalary; //基本工资
public Employee(String name, String phoneNumber, double baseSalary) {
this.name = name;
this.phoneNumber = phoneNumber;
this.baseSalary = baseSalary;
}
public double getMonthSalary() {
return baseSalary;
}
}
```
HourlyWorker 类:
```java
public class HourlyWorker extends Employee {
private double workHours; //每月工作小时数
private double perHourWage; //每小时费用
public HourlyWorker(String name, String phoneNumber, double workHours, double perHourWage) {
super(name, phoneNumber, 0);
this.workHours = workHours;
this.perHourWage = perHourWage;
}
@Override
public double getMonthSalary() {
return workHours * perHourWage;
}
}
```
SalesWorker 类:
```java
public class SalesWorker extends Employee {
private double commissionRate; //提成率
private double salesAmount; //月销售额
public SalesWorker(String name, String phoneNumber, double commissionRate, double salesAmount) {
super(name, phoneNumber, 0);
this.commissionRate = commissionRate;
this.salesAmount = salesAmount;
}
@Override
public double getMonthSalary() {
return baseSalary + commissionRate * salesAmount;
}
}
```
FixWageWorker 类:
```java
public class FixWageWorker extends Employee {
public FixWageWorker(String name, String phoneNumber, double baseSalary) {
super(name, phoneNumber, baseSalary);
}
}
```
EmployeeFactory 类:
```java
public class EmployeeFactory {
//方法一、创建员工对象的工厂方法
public static Employee createEmployee(String type, String name, String phoneNumber, double... args) {
switch (type) {
case "HourlyWorker":
return new HourlyWorker(name, phoneNumber, args[0], args[1]);
case "SalesWorker":
return new SalesWorker(name, phoneNumber, args[0], args[1]);
case "FixWageWorker":
return new FixWageWorker(name, phoneNumber, args[0]);
default:
throw new IllegalArgumentException("Invalid employee type: " + type);
}
}
//方法二、根据员工来输出此员工的当月工资
public static void printMonthSalary(Employee e) {
System.out.println("Name: " + e.name);
System.out.println("Phone number: " + e.phoneNumber);
System.out.println("Monthly salary: " + e.getMonthSalary());
}
}
```
UseEmployee 类:
```java
public class UseEmployee {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = EmployeeFactory.createEmployee("HourlyWorker", "Tom", "123456", 160.0, 20.0);
employees[1] = EmployeeFactory.createEmployee("SalesWorker", "Jerry", "654321", 0.1, 50000.0);
employees[2] = EmployeeFactory.createEmployee("FixWageWorker", "Alice", "111111", 10000.0);
for (Employee e : employees) {
EmployeeFactory.printMonthSalary(e);
}
}
}
```
阅读全文