完整java代码创建测试类EmployeeTest,在其main方法中分别输出技术员和销售员的姓名和总工资。给出员工参考信息Sales(200125, “tory”,32,2000);saleAmount 85
时间: 2024-03-11 17:46:43 浏览: 52
好的,下面是完整的Java代码创建测试类EmployeeTest并输出技术员和销售员的姓名和总工资:
```
public class EmployeeTest {
public static void main(String[] args) {
// 创建技术员对象
Technician technician = new Technician(10001, "张三", 30, 5000);
// 创建销售员对象
Sales sales = new Sales(200125, "tory", 32, 2000);
sales.setSaleAmount(85); // 设置销售额
// 输出技术员姓名和总工资
System.out.println("技术员姓名:" + technician.getName());
System.out.println("技术员总工资:" + technician.getTotalSalary());
// 输出销售员姓名和总工资
System.out.println("销售员姓名:" + sales.getName());
System.out.println("销售员总工资:" + sales.getTotalSalary());
}
}
// 技术员类
class Technician extends Employee {
public Technician(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
// 计算总工资(只包括基本工资)
public double getTotalSalary() {
return getSalary();
}
}
// 销售员类
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;
}
// 计算总工资(包括基本工资和提成)
public double getTotalSalary() {
return getSalary() + saleAmount * 0.05;
}
}
// 员工类
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 String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
```
在这个例子中,我们创建了三个类:Employee、Technician和Sales,其中Employee是所有员工的父类,而Technician和Sales是Employee的子类,分别代表技术员和销售员。在Employee类中,我们定义了员工的基本属性和方法,而在Technician和Sales类中,我们重写了Employee类中的getTotalSalary()方法,来计算技术员和销售员的总工资。最后,在EmployeeTest类中,我们创建了一个技术员对象和一个销售员对象,并分别输出了他们的姓名和总工资。
阅读全文