Java程序描述 按面向对象的理念,封装如下类型 员工类[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-24 12:18:34 浏览: 82
Java语言是由Sun公司于1995年5月23日正式推出的面向对象的程序设计语言
代码如下所示:
Employee类:
public class Employee {
private String name; //姓名
private String phoneNumber; //手机号
private double basicSalary; //基本工资
public Employee(String name, String phoneNumber, double basicSalary) {
this.name = name;
this.phoneNumber = phoneNumber;
this.basicSalary = basicSalary;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public double getBasicSalary() {
return basicSalary;
}
public double getMonthSalary() {
return basicSalary;
}
}
HourlyWorker类:
public class HourlyWorker extends Employee {
private int workHours; //每月工作小时数
private double hourSalary; //每小时费用
public HourlyWorker(String name, String phoneNumber, int workHours, double hourSalary) {
super(name, phoneNumber, 0);
this.workHours = workHours;
this.hourSalary = hourSalary;
}
@Override
public double getMonthSalary() {
return workHours * hourSalary;
}
}
SalesWorker类:
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 commissionRate * salesAmount;
}
}
FixWageWorker类:
public class FixWageWorker extends Employee {
public FixWageWorker(String name, String phoneNumber, double basicSalary) {
super(name, phoneNumber, basicSalary);
}
}
EmployeeFactory类:
public class EmployeeFactory {
//方法一、创建员工对象的工厂方法
public static Employee createEmployee(String type, String name, String phoneNumber, double... args) {
switch (type.toLowerCase()) {
case "hourlyworker":
return new HourlyWorker(name, phoneNumber, (int) 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("姓名:" + e.getName() + ",当月工资:" + e.getMonthSalary());
}
}
UseEmployee类:
public class UseEmployee {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = EmployeeFactory.createEmployee("hourlyworker", "张三", "123456789", 160, 50.0);
employees[1] = EmployeeFactory.createEmployee("salesworker", "李四", "987654321", 0.15, 10000.0);
employees[2] = EmployeeFactory.createEmployee("fixwageworker", "王五", "135792468", 5000.0);
for (Employee e : employees) {
EmployeeFactory.printMonthSalary(e);
}
}
}
运行结果:
姓名:张三,当月工资:8000.0
姓名:李四,当月工资:1500.0
姓名:王五,当月工资:5000.0
阅读全文