今天学习python实验,本次的实验为python中的实体类 ,随着实验的进行,让我发现它的实体类和c c++ java的实体类大同小异,只需要记住相关写法,着重记一点区别,就能很快适应python实体类的书写。
【题目描述】定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork。
程序中定义各种类的对象,并完成测试。
【练习要求】请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释。
# 定义人员类People
class People:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def __str__(self):
return f"姓名:{self.name},性别:{self.gender},年龄:{self.age}"
# 基于People实现学生类Student
class Student(People):
def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score):
super().__init__(name, gender, age)
self.student_id = student_id
self.enrollment_date = enrollment_date
self.entrance_score = entrance_score
def __str__(self):
return f"{super().__str__()},学号:{self.student_id},入学日期:{self.enrollment_date},入学成绩:{self.entrance_score}"
# 基于People实现教师类Teacher
class Teacher(People):
def __init__(self, name, gender, age, position, department, work_start_date):
super().__init__(name, gender, age)
self.position = position
self.department = department
self.work_start_date = work_start_date
def __str__(self):
return f"{super().__str__()},职位:{self.position},部门:{self.department},入职日期:{self.work_start_date}"
# 基于Student实现研究生类Graduate
class Graduate(Student):
def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score, reacher_direction, supervisor):
super().__init__(name, gender, age, student_id, enrollment_date, entrance_score)
self.reacher_direction = reacher_direction
self.supervisor = supervisor
def __str__(self):
return f"{super().__str__()},研究方向:{self.reacher_direction},指导老师:{self.supervisor}"
# 修改在职研究生类GradOnWork,体现既是学生也是教师的特点
class GradOnWork(Graduate):
def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score, reacher_direction, supervisor,
teacher_position="兼职讲师", teacher_department="兼职部门", teacher_work_start_date="工作时间"):
super().__init__(name, gender, age, student_id, enrollment_date, entrance_score, reacher_direction, supervisor)
self.teacher = Teacher(name, gender, age, teacher_position, teacher_department, teacher_work_start_date)
def __str__(self):
return f"{super().__str__()}\n,教师信息:{self.teacher}"
# 测试代码
student1 = Student("张三", "男", 20, "20223735", "2022-9-21", 555)
teacher1 = Teacher("李四", "男", 30, "讲师", "信息科学与技术", "2022-9-21")
graduate1 = Graduate("王五", "男", 25, "20223736", "2022-9-21", 555, "人工智能", "李四")
grad_on_work1 = GradOnWork("赵六", "男", 28, "20223737", "2022-9-21", 555, "人工智能", "李四", "兼职讲师",
"马克思主义学院", "2023-09-01")
print("学生信息 ")
print(student1)
print("\n教师信息 ")
print(teacher1)
print("\n研究生信息 ")
print(graduate1)
print("\n在职研究生信息 ")
print(grad_on_work1)