定义四个类,分别为Employee类、SalariedEmployee类、HourlyEmployee类和CommissionEmployee类。其中Employee类是其他三个类的父类。Employee类包含姓名和身份证号。除此之外,SalariedEmployee类还应包含每月工资;HourlyEmployee类还应包含每小时工资数和工作时间数;CommissionEmployee还应包含提成比例和销售总额。其中HourlyEmployee的工资为:每小时工资数×工作时间数,CommissionEmployee的工资为:提成比例×销售总额。每个类都应有合适的构造方法、数据成员的设置和读取方法。注意:子类有时需调用父类的构造方法和被覆盖的方法,成员变量定义为private,对有些方法实现重载。
时间: 2024-01-22 10:20:27 浏览: 120
以下是四个类的定义:
## Employee类
```java
public class Employee {
private String name;
private String id;
public Employee(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return this.name;
}
public String getId() {
return this.id;
}
}
```
## SalariedEmployee类
```java
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, String id, double monthlySalary) {
super(name, id);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return this.monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public double getSalary() {
return this.monthlySalary;
}
}
```
## HourlyEmployee类
```java
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int workHours;
public HourlyEmployee(String name, String id, double hourlySalary, int workHours) {
super(name, id);
this.hourlySalary = hourlySalary;
this.workHours = workHours;
}
public double getHourlySalary() {
return this.hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getWorkHours() {
return this.workHours;
}
public void setWorkHours(int workHours) {
this.workHours = workHours;
}
public double getSalary() {
return this.hourlySalary * this.workHours;
}
}
```
## CommissionEmployee类
```java
public class CommissionEmployee extends Employee {
private double commissionRate;
private double salesTotal;
public CommissionEmployee(String name, String id, double commissionRate, double salesTotal) {
super(name, id);
this.commissionRate = commissionRate;
this.salesTotal = salesTotal;
}
public double getCommissionRate() {
return this.commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
public double getSalesTotal() {
return this.salesTotal;
}
public void setSalesTotal(double salesTotal) {
this.salesTotal = salesTotal;
}
public double getSalary() {
return this.commissionRate * this.salesTotal;
}
}
```
以上是四个类的定义,它们分别表示不同类型的员工。每个类都有对应的数据成员和方法,可以方便地获取员工的信息和计算员工的薪水。其中,Employee类是其他三个类的父类,包含了所有员工都具有的基本信息。其他三个类则根据不同的情况增加了一些特定的信息和方法。
阅读全文