用java编写一个某公司的雇员分为以下若干类: (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会 额外奖励100 元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ① 属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加 上销售提成部分。 ① 属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打 印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2024-03-26 13:36:00 浏览: 210
魔乐李兴华老师JAVA基础课堂笔记(自写)
5星 · 资源好评率100%
好的,你的问题是如何用Java编写一个某公司的雇员分为不同类的程序,要求封装属性和方法,并能计算某个月的工资。我可以为你提供一个解决方案:
首先,我们需要创建一个Employee类,定义姓名和生日月份两个私有属性:
```java
public 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;
}
// getter和setter方法
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
public void setName(String name) {
this.name = name;
}
public void setBirthMonth(int birthMonth) {
this.birthMonth = birthMonth;
}
}
```
然后,我们需要创建四个子类分别对应不同类型的雇员。首先是SalariedEmployee类:
```java
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;
}
@Override
public double getSalary(int month) {
double salary = monthlySalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
接下来是HourlyEmployee类:
```java
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int monthlyWorkHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int monthlyWorkHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.monthlyWorkHours = monthlyWorkHours;
}
public double getHourlySalary() {
return hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getMonthlyWorkHours() {
return monthlyWorkHours;
}
public void setMonthlyWorkHours(int monthlyWorkHours) {
this.monthlyWorkHours = monthlyWorkHours;
}
@Override
public double getSalary(int month) {
double salary = hourlySalary * monthlyWorkHours;
if (monthlyWorkHours > 160) {
salary += (monthlyWorkHours - 160) * hourlySalary * 0.5;
}
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
然后是SalesEmployee类:
```java
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;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
最后是BasePlusSalesEmployee类:
```java
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;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
最后,我们可以创建一个Employee数组,并将不同类型的雇员对象添加到该数组中:
```java
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("Tom", 8, 5000);
employees[1] = new HourlyEmployee("Jerry", 10, 100, 180);
employees[2] = new SalesEmployee("Lucy", 5, 100000, 0.1);
employees[3] = new BasePlusSalesEmployee("Bob", 6, 80000, 0.08, 5000);
employees[4] = new SalariedEmployee("Alice", 9, 6000);
for (int i = 0; i < employees.length; i++) {
System.out.println(employees[i].getName() + " earns " + employees[i].getSalary(8) + " RMB in August.");
}
}
}
```
在这个例子中,我们创建了一个包含5个不同类型雇员的Employee数组,并且调用每个雇员的getSalary方法计算8月份的工资。结果会输出每个雇员的姓名和工资。
阅读全文