python编写 Person 类,其具有 name、age、sex 等属性。继承于 Person 类的 Teacher 类,具 有 title、quality、salary、prize 等属性。显示这些属性,并计算 salary、prize 之和。
时间: 2023-07-10 16:21:59 浏览: 322
python中类的属性和方法介绍
好的,下面是代码实现:
```python
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class Teacher(Person):
def __init__(self, name, age, sex, title, quality, salary, prize):
super().__init__(name, age, sex)
self.title = title
self.quality = quality
self.salary = salary
self.prize = prize
def display(self):
print("姓名:", self.name)
print("年龄:", self.age)
print("性别:", self.sex)
print("职称:", self.title)
print("教学质量:", self.quality)
print("工资:", self.salary)
print("奖金:", self.prize)
def total_income(self):
return self.salary + self.prize
# 测试代码
teacher = Teacher("张三", 35, "男", "教授", "优秀", 10000, 5000)
teacher.display()
print("总收入:", teacher.total_income())
```
输出结果为:
```
姓名: 张三
年龄: 35
性别: 男
职称: 教授
教学质量: 优秀
工资: 10000
奖金: 5000
总收入: 15000
```
阅读全文