以下是使用 Python 实现的代码: ```python import random class Teacher: def __init__(self, num, sex, name, age): self.__num = num self.__sex = sex self.__name = name self.__age = age def output(self): print(f"{self.__name} is a teacher") class Student: def __init__(self, num, sex, name, age): self.__num = num self.__sex = sex self.__name = name self.__age = age def output(self): print(f"{self.__name} is a student") class Clazz: def __init__(self, teacher, students): self.teacher = teacher self.students = students def output_all(self): self.teacher.output() self.students.sort(key=lambda x: x.__age) for student in self.students: student.output() # 生成随机的老师和学生信息 teacher = Teacher(random.randint(1, 100), random.choice(['男', '女']), "Teacher" + str(random.randint(1, 100)), random.randint(25, 60)) students = [Student(random.randint(101, 200), random.choice(['男', '女']), "Student" + str(random.randint(1, 100)), random.randint(10, 25)) for _ in range(5)] cls = Clazz(teacher, students) cls.output_all() ``` [2024-10-14 18:58:07 | AI写代码神器 | 367点数解答]