使用python 用面向对象实现学生信息管理代码
时间: 2024-04-30 15:24:25 浏览: 83
好的,以下是一个简单的学生信息管理系统的Python代码,基于面向对象的编程方法:
```python
class Student:
def __init__(self, name, id, age, grade):
self.name = name
self.id = id
self.age = age
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, name, id, age, grade):
student = Student(name, id, age, 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 print_all_students(self):
for student in self.students:
print(f"Name: {student.name}, ID: {student.id}, Age: {student.age}, Grade: {student.grade}")
# example usage:
manager = StudentManager()
manager.add_student("Alice", 1001, 18, 90)
manager.add_student("Bob", 1002, 19, 85)
manager.add_student("Charlie", 1003, 20, 95)
manager.print_all_students()
manager.remove_student(1002)
manager.print_all_students()
```
以上代码首先定义了一个`Student`类,表示一个学生的基本信息,包括姓名、学号、年龄和成绩。然后定义了一个`StudentManager`类,表示学生信息管理系统,包括添加学生、删除学生和打印所有学生信息等功能。通过创建`StudentManager`的实例来使用这个系统,如上面的示例代码所示。
阅读全文