(3)用类的组合技术,设计一个基本信息类、一个学生类、一个教师类。基本信息类包括姓名、性别、手机号、班级;学生类包括姓名、性别、手机号、班级、数学、语文和英语等成绩;教师类包括姓名、性别、手机号、班级、专业、教龄等。以上三个类必须包含构造函数,其他成员函数自行设计。主函数中,分别生成一个学生类和教师类对象,并调用成员函数。
时间: 2023-10-20 18:59:25 浏览: 61
学生类的构造函数
好的,我明白了。下面是基本信息类、学生类和教师类的代码实现:
```python
class BasicInfo:
def __init__(self, name, gender, phone_number, clazz):
self.name = name
self.gender = gender
self.phone_number = phone_number
self.clazz = clazz
class Student:
def __init__(self, name, gender, phone_number, clazz, math_score, chinese_score, english_score):
self.basic_info = BasicInfo(name, gender, phone_number, clazz)
self.math_score = math_score
self.chinese_score = chinese_score
self.english_score = english_score
def get_total_score(self):
return self.math_score + self.chinese_score + self.english_score
class Teacher:
def __init__(self, name, gender, phone_number, clazz, major, teaching_years):
self.basic_info = BasicInfo(name, gender, phone_number, clazz)
self.major = major
self.teaching_years = teaching_years
def get_teaching_age(self):
return self.teaching_years
```
在主函数中,我们可以生成一个学生类和一个教师类的对象,并调用它们的成员函数:
```python
if __name__ == '__main__':
student1 = Student('张三', '男', '13888888888', '高一一班', 90, 80, 85)
print(student1.basic_info.name)
print(student1.get_total_score())
teacher1 = Teacher('李老师', '女', '13999999999', '高一一班', '数学', 5)
print(teacher1.basic_info.name)
print(teacher1.get_teaching_age())
```
输出结果为:
```
张三
255
李老师
5
```
我们可以看到,基本信息类、学生类和教师类都已经成功地定义了,并且我们能够通过成员函数获取它们的信息。
阅读全文