优化这段代码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; } } class SalariedEmployee extends Employee { 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 == getBirthMonth()) { salary += 100; } return salary; } } class HourlyEmployee extends Employee { private double hourlyWage; private int hoursWorked; public HourlyEmployee(String name, int birthMonth, double hourlyWage, int hoursWorked) { super(name, birthMonth); this.hourlyWage = hourlyWage; this.hoursWorked = hoursWorked; } @Override public double getSalary(int month) { double salary = 0; if (hoursWorked <= 160) { salary = hoursWorked * hourlyWage; } else { salary = 160 * hourlyWage + (hoursWorked - 160) * hourlyWage * 1.5; } if (month == getBirthMonth()) { salary += 100; } return salary; } } 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; } @Override public double getSalary(int month) { double salary = monthlySales * commissionRate; if (month == getBirthMonth()) { salary += 100; } return salary; } } 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; } @Override public double getSalary(int month) { double salary = super.getSalary(month) + baseSalary; if (month == getBirthMonth()) { salary += 100; } return salary; } } public class P5 { public static void main(String[] args) { Employee[] employees = new Employee[5]; employees[0] = new SalariedEmployee("Tom", 5, 5000); employees[1] = new HourlyEmployee("Jerry", 12, 20, 180); employees[2] = new SalesEmployee("Mike", 8, 10000, 0.1); employees[3] = new BasePlusSalesEmployee("Lucy", 4, 8000, 0.2, 3000); employees[4] = new Employee("Jack", 9); int month = 5; for (Employee employee : employees) { System.out.println(employee.getSalary(month)); } } }
时间: 2024-03-31 08:36:15 浏览: 103
这段代码的主要问题是没有使用封装和抽象化的思想,导致代码可读性差、可维护性差,并且在修改时需要考虑多个类之间的关系。以下是一些优化建议:
1. 封装属性:将类的属性设置为私有,并提供相应的get和set方法来访问和修改属性,这样可以隐藏属性的具体实现细节,提高代码的可维护性和安全性。
2. 抽象化类:将Employee类设置为一个抽象类,并将getSalary方法设置为抽象方法,这样可以让子类来实现自己的getSalary方法,提高代码的可扩展性。
3. 使用接口:将Employee类设置为一个接口,并将getSalary方法设置为默认方法,这样可以让子类来实现自己的getSalary方法,同时也可以让其他类实现Employee接口,提高代码的可复用性。
4. 使用枚举类型:将月份设置为一个枚举类型,这样可以避免使用魔法数字,提高代码的可读性和可维护性。
5. 使用工厂模式:使用工厂模式来创建不同的Employee子类对象,这样可以封装对象的创建过程,提高代码的可扩展性和可维护性。
6. 使用Lambda表达式:使用Lambda表达式来简化代码,提高代码的可读性和可维护性。例如可以使用Lambda表达式来计算小时工的工资。
下面是优化后的代码:
相关问题
某公司的雇员分为3类,每类员工都有相应的封装类,类的信息如下所示。 (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) ,参数为月份,如果该月是员工的生日,则公司会额外奖励100 元,否则默认返回0。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (4) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 ① 属性:底薪。 (5)另创建一个Company类,该类有一个属性为Employee数组,用于存储公司的员工对象。有一个displaySalay(int month),输出:xxx(姓名)x月的工资是:xxx.xx元(保留两位小数) 本题要求根据上述雇员分类,编写一个程序,满足一下要求: (1)不允许有public和默认属性,根据需求,使用protected和private修饰属性; (2)每个类必须有一个全参数的构造函数; (3)合理使用this和super关键字访问属性、方法和构造函数; (4)在main方法里创建若干个Employee的子类的对象,并将这些存入一个Employee 数组; (5)以步骤(4)中的Employee数组为参数,创建一个Company的对象,调用该对象的displaySalay(int month)方法输出所有员工某月的工资。
好的,您的问题是要求编写一个关于某公司员工封装类的程序,我可以提供一份示例代码供您参考。
```java
import java.text.DecimalFormat;
// Employee类
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) {
return (month == birthMonth) ? 100 : 0;
}
public String getName() {
return name;
}
}
// SalariedEmployee类
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getSalary(int month) {
return super.getSalary(month) + monthlySalary;
}
}
// SalesEmployee类
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 getSalary(int month) {
return super.getSalary(month) + monthlySales * commissionRate;
}
}
// BasePlusSalesEmployee类
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 getSalary(int month) {
return super.getSalary(month) + baseSalary;
}
}
// Company类
class Company {
private Employee[] employees;
public Company(Employee[] employees) {
this.employees = employees;
}
public void displaySalary(int month) {
DecimalFormat df = new DecimalFormat("0.00");
for (Employee employee : employees) {
double salary = employee.getSalary(month);
System.out.println(employee.getName() + month + "的工资是:" + df.format(salary));
}
}
}
// 测试程序
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("张三", 3, 5000);
employees[1] = new SalesEmployee("李四", 4, 10000, 0.05);
employees[2] = new BasePlusSalesEmployee("王五", 5, 8000, 0.03, 3000);
employees[3] = new SalariedEmployee("赵六", 6, 6000);
Company company = new Company(employees);
company.displaySalary(3);
}
}
```
在这份示例代码中,我们定义了5个类,分别是Employee、SalariedEmployee、SalesEmployee、BasePlusSalesEmployee、Company。其中,Employee是所有员工的父类,其他4个类都是Employee的子类。
在main方法中,我们创建了4个Employee的子类的对象,并将这些存入一个Employee数组。然后,我们以该Employee数组为参数,创建了一个Company的对象,并调用该对象的displaySalary(int month)方法输出所有员工某月的工资。
在Employee类中,我们定义了name和birthMonth两个属性,以及getSalary(int month)方法。其中,getSalary(int month)方法判断该月是否是员工的生日,如果是,则公司会额外奖励100元,否则默认返回0元。
在SalariedEmployee、SalesEmployee和BasePlusSalesEmployee类中,我们分别定义了monthlySalary、monthlySales、commissionRate和baseSalary等属性,以及继承自Employee类的getSalary(int month)方法,并在该方法中调用了父类的getSalary(int month)方法,然后加上了员工的固定工资、销售提成和底薪等部分。
在Company类中,我们定义了employees属性,该属性用于存储公司的员工对象。我们还定义了displaySalary(int month)方法,该方法遍历了所有员工对象,并调用了各自的getSalary(int month)方法,最后输出员工的姓名和工资。
希望这份示例代码能够帮助到您!
某公司的雇员分为3类,每类员工都有相应的封装类,类的信息如下所示。(1) Employee:这是所有员工总的父类。① 属性:员工的姓名,员工的生日月份② 方法:getSalary(int month) ,参数为月份,如果该月是员工的生日,则公司会额外奖励100 元,否则默认返回0。(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。① 属性:月薪。(3) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。① 属性:月销售额、提成率。(4) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。① 属性:底薪。(5)另创建一个Company类,该类有一个属性为Employee数组,用于存储公司的员工对象。有一个displaySalay(int month),输出:xxx(姓名)x月的工资是:xxx.xx元(保留两位小数)本题要求根据上述雇员分类,编写一个程序,满足一下要求:(1)不允许有public和默认属性,根据需求,使用protected和private修饰属性;(2)每个类必须有一个全参数的构造函数;(3)合理使用this和super关键字访问属性、方法和构造函数;(4)在main方法里创建若干个Employee的子类的对象,并将这些存入一个Employee 数组;(5)以步骤(4)中的Employee数组为参数,创建一个Company的对象,调用该对象的displaySalay(int month)方法输出所有员工某月的工资。
好的,我理解了你的问题。下面是代码实现:
```java
import java.text.DecimalFormat;
abstract class Employee {
private String name; //员工姓名
private int birthMonth; //员工生日月份
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
public abstract double getSalary(int month); //计算工资的抽象方法
}
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;
if(getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
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;
if(getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
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 = super.getSalary(month) + baseSalary;
return salary;
}
}
class Company {
private Employee[] employees;
public Company(Employee[] employees) {
this.employees = employees;
}
public void displaySalary(int month) {
DecimalFormat df = new DecimalFormat("0.00");
for(Employee employee : employees) {
System.out.println(employee.getName() + " " + month + "月的工资是:" + df.format(employee.getSalary(month)) + "元");
}
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("张三", 8, 5000);
employees[1] = new SalesEmployee("李四", 6, 10000, 0.1);
employees[2] = new BasePlusSalesEmployee("王五", 5, 20000, 0.05, 3000);
employees[3] = new SalariedEmployee("赵六", 11, 6000);
Company company = new Company(employees);
company.displaySalary(6);
}
}
```
代码中,我们首先定义了一个抽象类 `Employee`,包含员工的姓名和生日月份属性以及计算工资的抽象方法 `getSalary`。然后我们定义了三个员工类 `SalariedEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee`,它们都继承自 `Employee`。`SalariedEmployee` 表示拿固定工资的员工,有月薪属性;`SalesEmployee` 表示销售,有月销售额和提成率属性;`BasePlusSalesEmployee` 表示有固定底薪的销售人员,有底薪属性。这三个类都实现了 `getSalary` 方法,根据具体情况计算员工的工资。
最后,我们创建了一个 `Company` 类,包含一个 `Employee` 数组,用于存储公司的员工对象。该类有一个 `displaySalary` 方法,遍历数组中的每个员工对象,调用 `getSalary` 方法计算该员工在指定月份的工资,输出格式为“姓名 月份的工资是:xxx.xx元”。
在 `main` 方法中,我们创建了四个员工对象并存入一个 `Employee` 数组中,然后创建了一个 `Company` 对象,调用其 `displaySalary` 方法输出所有员工在6月份的工资。
阅读全文