2. 简答题 写一个雇员类(姓名,年龄,薪水),一个经理类(姓名,年龄,薪水,管理的人数),每个雇员有管理他的经理,每个经理有管理的员工,在测试内中通过雇员“李四”找到其经理“张三”的信息;通过经理“张三”找到他管理的所有雇员的信息。
时间: 2024-10-09 20:14:56 浏览: 27
为了实现这个需求,我们可以创建两个类:`Employee` 和 `Manager`,并使用嵌套类的关系来代表管理层次结构。这里是一个简单的Python实现示例:
```python
class Employee:
def __init__(self, name, age, salary, manager=None):
self.name = name
self.age = age
self.salary = salary
self.manager = manager
def get_manager(self):
return self.manager
@classmethod
def find_employee_by_name(cls, employee_name, manager_name):
# 这里只是一个简化的查找示例,实际应用中需要存储所有员工数据
for e in cls.all_employees:
if e.name == employee_name and e.get_manager() is not None and e.get_manager().name == manager_name:
return e
return None
class Manager(Employee):
all_employees = []
def __init__(self, name, age, salary, num_employees=0):
super().__init__(name, age, salary)
self.num_employees = num_employees
self.employees = []
def add_employee(self, employee):
self.employees.append(employee)
employee.manager = self
Manager.all_employees.append(employee)
def get_employee_info(self):
return [e for e in self.employees]
# 示例测试
manager_zhangsan = Manager("张三", 35, 8000, 2)
employee_liisi = Employee("李四", 28, 6000, manager_zhangsan)
manager_zhangsan.add_employee(employee_liisi)
# 查找员工信息
employee_info = Employee.find_employee_by_name("李四", "张三")
if employee_info:
print(f"员工{employee_info.name}的经理是{employee_info.manager.name}")
else:
print("找不到匹配的员工")
# 查找经理信息
all_employees = manager_zhangsan.get_employee_info()
print(f"经理{manager_zhangsan.name}管理的员工信息:")
for e in all_employees:
print(e.name)
```
阅读全文