使用继承创建学校成员的类。学校成员包含老师和学生,老师和学生均包含姓名和年龄,最终展示老师的姓名、年龄及薪水,学生的姓名、年龄及得分。
时间: 2024-03-05 22:53:14 浏览: 14
可以使用继承来创建学校成员的类。下面是一个示例代码,其中包含了一个父类Person,以及两个子类Teacher和Student。Teacher和Student类均继承了Person类,并在其基础上添加了薪水和得分属性。
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Teacher(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.salary = salary
def display(self):
print("Name:", self.name)
print("Age:", self.age)
print("Salary:", self.salary)
class Student(Person):
def __init__(self, name, age, score):
super().__init__(name, age)
self.score = score
def display(self):
print("Name:", self.name)
print("Age:", self.age)
print("Score:", self.score)
```
在上面的代码中,Person类包含了姓名和年龄属性。Teacher和Student类继承了Person类,并在其基础上添加了薪水和得分属性。Teacher类还实现了一个display()方法来展示老师的姓名、年龄和薪水。Student类也实现了一个display()方法来展示学生的姓名、年龄和得分。