1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| """ 4.练习: 有一个学校,人数为0,入职的老师和学生,人数增加1,老师要显示姓名和工号, 学生要显示姓名和成绩,老师和学生入职后都需要做自我介绍 """ class people(): def __init__(self, name, id): self.name = name self.id = id def introduction(self): print(f"大家好,我是{self.name}")
class teacher(people): def __init__(self, name, id): super(teacher, self).__init__(name, id) def __str__(self): msg = f"姓名:{self.name} 工号:{self.id}" return msg class student(people): def __init__(self, name, id, scores): self.scores = scores super(student, self).__init__(name, id) def __str__(self): msg = f"姓名:{self.name} 学号:{self.id} 成绩:{self.scores}" return msg
class school(): def __init__(self): self.total_num = 0 self.teachers = [] self.students = [] def add_teachers(self, teacher): self.teachers.append(teacher) self.total_num += 1 print(teacher) teacher.introduction() def add_student(self, student): self.students.append(student) self.total_num += 1 print(student) student.introduction()
jinan_university = school() teacherA = teacher("xiazhihua", "0001") studentA = student("ztb", "123", [89, 89, 89]) jinan_university.add_teachers(teacherA) jinan_university.add_student(studentA)
|