土、编稈颢 某公司的雇员分为以下若千类: (1)Employee:这是所有员工总的父类。 ①属性:员工的姓名,员工的生日月份 ②方法:getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。 (2) SalariedEmployee: Employee 的子类,拿固定工资的员工。 ①属性:月薪。 (3)HourlyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ①属性:每小时的工资、每月工作的小时数。 (4) SalesEmplovee: Employee 的子类,销售,工资由月销售额和提成率决定。 ①属性:月销售额、提成率。 (5) BasePlusSalesEmployee : SalesEmployee 的子 类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 ①属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的 Employee对象,并打印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属
时间: 2024-03-10 17:44:22 浏览: 24
以下是一个可行的Java程序实现:
```
public 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 double getSalary(int month) {
double salary = 0;
return salary;
}
}
public 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;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public double getSalary(int month) {
double salary = monthlySalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class HourlyEmployee extends Employee {
private double hourlyWage;
private double monthlyHours;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, double monthlyHours) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.monthlyHours = monthlyHours;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public double getMonthlyHours() {
return monthlyHours;
}
public void setMonthlyHours(double monthlyHours) {
this.monthlyHours = monthlyHours;
}
public double getSalary(int month) {
double salary = 0;
if (monthlyHours <= 160) {
salary = monthlyHours * hourlyWage;
} else {
salary = 160 * hourlyWage + (monthlyHours - 160) * hourlyWage * 1.5;
}
return salary;
}
}
public 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 void setMonthlySales(double monthlySales) {
this.monthlySales = monthlySales;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
return salary;
}
}
public 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;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
return salary;
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("Tom", 3, 5000);
employees[1] = new HourlyEmployee("Jack", 5, 50, 180);
employees[2] = new SalesEmployee("Lucy", 6, 10000, 0.1);
employees[3] = new BasePlusSalesEmployee("Mike", 12, 8000, 0.08, 2000);
employees[4] = new HourlyEmployee("Mary", 8, 60, 150);
int month = 5;
for (Employee e : employees) {
System.out.println(e.getName() + "'s salary in month " + month + " is " + e.getSalary(month));
}
}
}
```
这个程序创建了Employee及其子类的类,实现了getSalary方法,然后在Test类中创建了一个Employee数组,分别创建了不同类型的员工对象,并调用getSalary方法计算某个月的工资。