5.28
(一)、设计高校人员信息包,并完成测试
【题目描述】定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork。
【源代码程序】
class People:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def display_info(self):
print(f"姓名: {self.name}, 性别: {self.gender}, 年龄: {self.age}")
class Student(People):
def __init__(self, name, gender, age, student_id, enrollment_year, admission_score):
super().__init__(name, gender, age)
self.student_id = student_id
self.enrollment_year = enrollment_year
self.admission_score = admission_score
def display_student_info(self):
super().display_info()
print(f"学号: {self.student_id}, 入学年份: {self.enrollment_year}, 录取分数: {self.admission_score}")
class Teacher(People):
def __init__(self, name, gender, age, title, department, working_years):
super().__init__(name, gender, age)
self.title = title
self.department = department
self.working_years = working_years
def display_teacher_info(self):
super().display_info()
print(f"职务: {self.title}, 部门: {self.department}, 工作时间: {self.working_years}")
class Graduate(Student):
def __init__(self, name, gender, age, student_id, enrollment_year, admission_score, research_direction, advisor):
super().__init__(name, gender, age, student_id, enrollment_year, admission_score)
self.research_direction = research_direction
self.advisor = advisor
def display_graduate_info(self):
super().display_student_info()
print(f"研究方向: {self.research_direction}, 导师: {self.advisor}")
class GradOnWork(Graduate):
def __init__(self, name, gender, age, student_id, enrollment_year, admission_score, research_direction, advisor, work_status):
super().__init__(name, gender, age, student_id, enrollment_year, admission_score, research_direction, advisor)
self.work_status = work_status
def display_work_status(self):
print(f"工作状态: {self.work_status}")
# 重写显示信息方法,包含工作状态
def display_graduate_info(self):
super().display_graduate_info()
self.display_work_status()
if __name__ == "__main__":
# 创建一个研究生实例
grad_student = Graduate("索尔古德曼", "男", 24, "S2023001", 2023, 85.0, "文科", "Dr.布吉岛")
grad_student.display_graduate_info()
# 创建一个在职研究生实例
grad_on_work = GradOnWork("沃尔特怀特", "男", 27, "S2021002", 2021, 90.0, "结晶学", "Prof.海森堡", "在职研究生")
grad_on_work.display_graduate_info()