定义两种特殊的职工,技术员(Technician)和销售员(Sales) (1)技术员(Technician)包含特殊属性“工时(hours)” 其总工资为:基本工资+2*hours (2)销售员(Sales)包含特殊属性“销售量(saleAmount)” 其总工资为:基本工资+4*saleAmount
时间: 2024-01-03 15:03:34 浏览: 154
好的,以下是关于技术员(Technician)和销售员(Sales)的类的代码实现,其中包含了各自的特殊属性和总工资计算方法:
```java
public abstract class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 calculateSalary();
}
public class Technician extends Employee {
private int hours;
public Technician(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
@Override
public double calculateSalary() {
return getSalary() + 2 * hours;
}
}
public class Sales extends Employee {
private double saleAmount;
public Sales(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
public double getSaleAmount() {
return saleAmount;
}
public void setSaleAmount(double saleAmount) {
this.saleAmount = saleAmount;
}
@Override
public double calculateSalary() {
return getSalary() + 4 * saleAmount;
}
}
```
其中,Employee 是一个抽象类,包含了所有员工都拥有的属性和方法,calculateSalary() 是一个抽象方法,需要在子类中分别实现。Technician 和 Sales 分别继承自 Employee,包含了各自的特殊属性和实现了 calculateSalary() 方法。在 Technician 中,特殊属性是工时(hours),总工资计算方式是基本工资加上工时乘以2;在 Sales 中,特殊属性是销售量(saleAmount),总工资计算方式是基本工资加上销售量乘以4。
阅读全文