写一个学生信息管理系统代码
时间: 2023-12-17 18:49:16 浏览: 32
以下是一个简单的学生信息管理系统代码,使用 Python 语言编写:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student_by_name(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
return True
return False
def get_all_students(self):
return self.students
def get_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
# 测试代码
manager = StudentManager()
# 添加学生
student1 = Student("Tom", 18, "male", "Grade 1")
manager.add_student(student1)
student2 = Student("Lucy", 17, "female", "Grade 2")
manager.add_student(student2)
# 获取所有学生
all_students = manager.get_all_students()
for student in all_students:
print(student.name, student.age, student.gender, student.grade)
# 根据姓名获取学生
student = manager.get_student_by_name("Tom")
print(student.name, student.age, student.gender, student.grade)
# 根据姓名删除学生
result = manager.remove_student_by_name("Lucy")
if result:
print("删除成功!")
else:
print("删除失败!")
```
这个学生信息管理系统包含了 `Student` 和 `StudentManager` 两个类,其中 `Student` 类表示一个学生的基本信息,包括姓名、年龄、性别和班级;而 `StudentManager` 类则负责管理学生信息,包括添加、删除、查询等操作。在测试代码中,我们创建了两个学生对象并添加到管理器中,然后获取所有学生信息、根据姓名查询学生信息以及根据姓名删除学生信息。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)