根据以下要求进行编程 在公司信息系统中,包括: (1) 员工,其可能的信息包括: 属性:employeeNumber、姓名、地址、电话号码。 方法:computePay(),构造方法。 (2) Magagers除了普通员工的属性和行为外,还具有以下属性和行为: 添加属性:职责,员工列表 行为:computePay()(),构造方法。 (3) 在测试类中重写toString()、输出经理的姓名、部门和工资(保留两位小数)。
时间: 2023-05-21 19:06:50 浏览: 62
以下是用Java语言实现的代码:
```
public class Employee {
public int employeeNumber;
public String name;
public String address;
public String phoneNumber;
public Employee(int employeeNumber, String name, String address, String phoneNumber) {
this.employeeNumber = employeeNumber;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public void computePay() {}
public String toString() {
return "Employee name: " + name;
}
}
public class Manager extends Employee {
public String responsibility;
public ArrayList<Employee> employeeList;
public Manager(int employeeNumber, String name, String address, String phoneNumber, String responsibility, ArrayList<Employee> employeeList) {
super(employeeNumber, name, address, phoneNumber);
this.responsibility = responsibility;
this.employeeList = employeeList;
}
public void computePay() {}
public String toString() {
return "Manager name: " + name + ", Department: " + responsibility + ", Salary: " + String.format("%.2f", computeTotalSalary());
}
public double computeTotalSalary() {
double totalSalary = 0;
for (Employee employee : employeeList) {
totalSalary += employee.computePay();
}
return totalSalary;
}
}
public class Test {
public static void main(String[] args) {
Employee employee1 = new Employee(1, "John", "123 Main St", "123-456-7890");
ArrayList<Employee> employeeList = new ArrayList<>();
employeeList.add(employee1);
Manager manager1 = new Manager(2, "Jane", "456 Elm St", "234-567-8901", "Sales", employeeList);
System.out.println(manager1.toString());
}
}
```
在测试类中,我们创建了一个Employee对象和一个Manager对象,然后将Employee对象添加到Manager的员工列表中。最后,我们调用Manager的toString()方法来输出其姓名、部门和工资。其中,computeTotalSalary()是一个返回所有员工工资总和的辅助方法。
阅读全文