用python写一个智慧校园
时间: 2023-05-17 12:02:58 浏览: 159
非常感谢您的提问!以下是一个简单的智慧校园的 Python 代码示例:
```
class SmartCampus:
def __init__(self, name, location):
self.name = name
self.location = location
self.students = []
self.teachers = []
self.courses = []
def add_student(self, student):
self.students.append(student)
def add_teacher(self, teacher):
self.teachers.append(teacher)
def add_course(self, course):
self.courses.append(course)
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, gender, grade):
super().__init__(name, age, gender)
self.grade = grade
class Teacher(Person):
def __init__(self, name, age, gender, subject):
super().__init__(name, age, gender)
self.subject = subject
class Course:
def __init__(self, name, teacher, students):
self.name = name
self.teacher = teacher
self.students = students
campus = SmartCampus("ABC School", "Beijing")
student1 = Student("Tom", 15, "male", 9)
student2 = Student("Lucy", 14, "female", 8)
teacher1 = Teacher("Mr. Smith", 35, "male", "Math")
teacher2 = Teacher("Ms. Li", 30, "female", "English")
course1 = Course("Math 101", teacher1, [student1, student2])
course2 = Course("English 101", teacher2, [student1])
campus.add_student(student1)
campus.add_student(student2)
campus.add_teacher(teacher1)
campus.add_teacher(teacher2)
campus.add_course(course1)
campus.add_course(course2)
```
这个代码示例创建了一个智慧校园的类 `SmartCampus`,以及学生、教师和课程的类 `Student`、`Teacher` 和 `Course`。在 `SmartCampus` 类中,我们可以添加学生、教师和课程,以及获取校园的名称和位置。在 `Student` 和 `Teacher` 类中,我们可以获取他们的姓名、年龄、性别和其他属性。在 `Course` 类中,我们可以获取课程的名称、教师和学生。
希望这个代码示例能够帮助您!
阅读全文