eplices中写一个学生管理系统
时间: 2024-04-30 19:19:24 浏览: 71
好的,以下是一个简单的学生管理系统的代码示例:
```python
class Student:
def __init__(self, name, id, grade):
self.name = name
self.id = id
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, id, grade):
student = Student(name, id, grade)
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def get_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def get_all_students(self):
return self.students
```
使用示例:
```python
# 创建一个学生管理系统对象
sms = StudentManagementSystem()
# 添加学生
sms.add_student("张三", "001", 80)
sms.add_student("李四", "002", 90)
sms.add_student("王五", "003", 85)
# 获取所有学生
all_students = sms.get_all_students()
for student in all_students:
print(student.name, student.id, student.grade)
# 获取指定学生
student = sms.get_student("002")
print(student.name, student.id, student.grade)
# 删除学生
result = sms.remove_student("003")
if result:
print("删除成功")
else:
print("删除失败")
```
这是一个简单的学生管理系统,包含了添加学生、删除学生、获取指定学生和获取所有学生的功能。你可以根据自己的需求,对其进行扩展。
阅读全文