python用面向对象写学生管理系统
时间: 2023-07-18 16:29:57 浏览: 50
好的,我可以为您提供一个简单的学生管理系统的面向对象代码示例。
```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):
new_student = Student(name, id, grade)
self.students.append(new_student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def update_student(self, id, name=None, grade=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if grade:
student.grade = grade
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def print_all_students(self):
for student in self.students:
print("Name:", student.name)
print("ID:", student.id)
print("Grade:", student.grade)
print("===")
# Example usage
system = StudentManagementSystem()
system.add_student("Alice", "001", 85)
system.add_student("Bob", "002", 92)
system.print_all_students()
system.update_student("001", name="Alicia", grade=90)
system.print_all_students()
system.remove_student("002")
system.print_all_students()
```
这个代码示例定义了两个类:`Student` 和 `StudentManagementSystem`。`Student` 类表示一个学生,它有三个属性:`name` (姓名)、`id` (学号) 和 `grade` (成绩)。`StudentManagementSystem` 类为学生管理系统提供了一些方法,如添加学生、删除学生、更新学生信息、查找学生和打印所有学生信息等。
你可以根据自己的需求,扩展这个代码示例,添加更多的功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)