python用类写学生管理系统
时间: 2023-09-24 21:03:34 浏览: 61
好的,以下是一个简单的学生管理系统的代码示例:
```python
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
class StudentManagement:
def __init__(self):
self.students = []
def add_student(self, name, grade):
student = Student(name, grade)
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
def print_students(self):
print("Students:")
for student in self.students:
print("Name: {}, Grade: {}".format(student.name, student.grade))
# Example usage
management = StudentManagement()
management.add_student("Alice", 85)
management.add_student("Bob", 70)
management.add_student("Charlie", 92)
management.print_students()
management.remove_student("Bob")
management.print_students()
```
这段代码定义了两个类:`Student` 和 `StudentManagement`。`Student` 类表示一个学生,包含了学生的姓名和成绩。`StudentManagement` 类表示学生管理系统,包含了学生列表以及添加、删除和打印学生信息的方法。在这个例子中,我们创建了一个 `StudentManagement` 对象,添加了三个学生,打印了学生信息,然后删除了一个学生,再次打印学生信息。
阅读全文