用python代码实现- name: string - email: string + get_info(): string } class Student { - courses: List<Course> + add_course(course: Course) + remove_course(course: Course) + get_courses(): List<Course> + get_info(): string } class Teacher { - courses: List<Course> + add_course(course: Course) + remove_course(course: Course) + get_courses(): List<Course> + get_info(): string } class Admin { + add_student(student: Student) + remove_student(student: Student) + add_teacher(teacher: Teacher) + remove_teacher(teacher: Teacher) + add_course(course: Course) + remove_course(course: Course) } class Course { - name: string - teacher: Teacher - students: List<Student> + get_info(): string } Student *-- Course Teacher *-- Course Course "1"--o "1" Teacher Course "1"--o "1..*" Student Admin ..> Person Person <|-- Student Person <|-- Teacher Person <|-- Admin
时间: 2024-03-09 14:47:22 浏览: 95
以下是实现该类的Python代码:
```python
from typing import List
class Person:
def __init__(self, name: str, email: str):
self.name = name
self.email = email
def get_info(self) -> str:
return f"Name: {self.name}\nEmail: {self.email}"
class Student(Person):
def __init__(self, name: str, email: str):
super().__init__(name, email)
self.courses = []
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
def get_courses(self) -> List[str]:
return [course.name for course in self.courses]
def get_info(self) -> str:
return f"{super().get_info()}\nCourses: {', '.join(self.get_courses())}"
class Teacher(Person):
def __init__(self, name: str, email: str):
super().__init__(name, email)
self.courses = []
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
def get_courses(self) -> List[str]:
return [course.name for course in self.courses]
def get_info(self) -> str:
return f"{super().get_info()}\nCourses: {', '.join(self.get_courses())}"
class Admin:
def __init__(self):
self.students = []
self.teachers = []
self.courses = []
def add_student(self, student: Student):
self.students.append(student)
def remove_student(self, student: Student):
self.students.remove(student)
def add_teacher(self, teacher: Teacher):
self.teachers.append(teacher)
def remove_teacher(self, teacher: Teacher):
self.teachers.remove(teacher)
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
class Course:
def __init__(self, name: str, teacher: Teacher):
self.name = name
self.teacher = teacher
self.students = []
def add_student(self, student: Student):
self.students.append(student)
def remove_student(self, student: Student):
self.students.remove(student)
def get_info(self) -> str:
return f"Name: {self.name}\nTeacher: {self.teacher.name}\nStudents: {[student.name for student in self.students]}"
# Example Usage
admin = Admin()
teacher = Teacher("John Smith", "john.smith@example.com")
course = Course("Math", teacher)
student = Student("Jane Doe", "jane.doe@example.com")
admin.add_teacher(teacher)
admin.add_student(student)
admin.add_course(course)
course.add_student(student)
teacher.add_course(course)
student.add_course(course)
print(student.get_info())
print(teacher.get_info())
print(course.get_info())
```
阅读全文