分别定义一个教师类和干部类,采用多重继承来派生一个新类一双职教师类。假设要管理下述几类人员的一些相关数据:
时间: 2024-05-10 11:18:42 浏览: 179
下面是教师类和干部类的定义:
```python
class Teacher:
def __init__(self, name, age, gender, subject):
self.name = name
self.age = age
self.gender = gender
self.subject = subject
def teach(self):
print(f"{self.name} is teaching {self.subject}.")
class Cadre:
def __init__(self, name, age, gender, position):
self.name = name
self.age = age
self.gender = gender
self.position = position
def manage(self):
print(f"{self.name} is managing {self.position}.")
```
接下来,我们可以用多重继承来派生出双职教师类:
```python
class DualRoleTeacher(Teacher, Cadre):
def __init__(self, name, age, gender, subject, position):
Teacher.__init__(self, name, age, gender, subject)
Cadre.__init__(self, name, age, gender, position)
```
这个双职教师类继承了教师类和干部类的属性和方法,同时也可以额外定义自己的属性和方法。这样,我们就可以用一个类来管理多种不同类型的人员了。比如:
```python
drt = DualRoleTeacher("Alice", 40, "female", "Math", "Department Head")
drt.teach()
drt.manage()
```
这段代码会输出:
```
Alice is teaching Math.
Alice is managing Department Head.
```
双职教师既可以教课,也可以管理部门。
阅读全文