Java编程 定义一个职工(Employee)抽象类,要求如下: (1)包含四个属性:员工号(eID)、姓名(name)、年龄(age),基本工资(salary)。(2)包含一个能初始化所有四个属性的构造函数。 (3)包含所有属性的getter和setter方法。 (4)包含一个getTotalSalary的抽象方法,返回员工的总工资。 2、(10分)定义两种特殊的职工,技术员(Technician)和销售员(Sales)(1)技术员(Technician)包含特殊属性“工时(hours)” 其总工资为:基本工资+2*hours (2)销售员(Sales)包含特殊属性“销售量(saleAmount)” 其总工资为:基本工资+4*saleAmount 3、(10分)创建测试类EmployeeTest,在其main方法中分别输出 技术员和销售员 的姓名积总工资。 给出员工的参考信息:(数据不要求相同)Sales(200125,"Tony",32,2000); saleAmount=85 Technician(200153,"Paul",33,3500);hours=160
时间: 2024-01-03 10:03:34 浏览: 179
下面是Java代码实现:
```java
// 抽象类Employee
public abstract class Employee {
private int eID; // 员工号
private String name; // 姓名
private int age; // 年龄
private double salary; // 基本工资
public Employee(int eID, String name, int age, double salary) {
this.eID = eID;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getEID() {
return eID;
}
public void setEID(int eID) {
this.eID = eID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public abstract double getTotalSalary(); // 抽象方法,获取员工总工资
}
// 技术员类Technician
public class Technician extends Employee {
private int hours; // 工时
public Technician(int eID, String name, int age, double salary, int hours) {
super(eID, name, age, salary);
this.hours = hours;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
@Override
public double getTotalSalary() {
return getSalary() + 2 * hours;
}
}
// 销售员类Sales
public class Sales extends Employee {
private int saleAmount; // 销售量
public Sales(int eID, String name, int age, double salary, int saleAmount) {
super(eID, name, age, salary);
this.saleAmount = saleAmount;
}
public int getSaleAmount() {
return saleAmount;
}
public void setSaleAmount(int saleAmount) {
this.saleAmount = saleAmount;
}
@Override
public double getTotalSalary() {
return getSalary() + 4 * saleAmount;
}
}
// 测试类EmployeeTest
public class EmployeeTest {
public static void main(String[] args) {
Sales sales = new Sales(200125, "Tony", 32, 2000, 85);
Technician technician = new Technician(200153, "Paul", 33, 3500, 160);
System.out.println(sales.getName() + "的总工资为:" + sales.getTotalSalary());
System.out.println(technician.getName() + "的总工资为:" + technician.getTotalSalary());
}
}
```
输出结果为:
```
Tony的总工资为:3400.0
Paul的总工资为:5180.0
```
阅读全文