python面向对象的学生信息管理系统
时间: 2024-01-10 07:21:59 浏览: 68
python学生信息管理系统
以下是一个简单的Python面向对象的学生信息管理系统的示例:
```python
# 定义学生类
class Student(object):
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# 定义学生管理系统类
class StudentManager(object):
def __init__(self):
# 存储学员数据 -- 列表
self.student_list = []
# 添加学员
def add_student(self, student):
self.student_list.append(student)
print("学员添加成功!")
# 删除学员
def delete_student(self, student):
if student in self.student_list:
self.student_list.remove(student)
print("学员删除成功!")
else:
print("学员不存在!")
# 修改学员信息
def modify_student(self, student, new_name, new_age, new_gender):
if student in self.student_list:
student.name = new_name
student.age = new_age
student.gender = new_gender
print("学员信息修改成功!")
else:
print("学员不存在!")
# 查询学员信息
def query_student(self, student):
if student in self.student_list:
print("学员姓名:", student.name)
print("学员年龄:", student.age)
print("学员性别:", student.gender)
else:
print("学员不存在!")
# 显示所有学员信息
def show_all_students(self):
for student in self.student_list:
print("学员姓名:", student.name)
print("学员年龄:", student.age)
print("学员性别:", student.gender)
# 保存学员信息
def save_students(self):
# 将学员信息保存到文件中
with open("students.txt", "w") as f:
for student in self.student_list:
f.write(student.name + "," + str(student.age) + "," + student.gender + "\n")
print("学员信息保存成功!")
# 退出系统
def exit_system(self):
self.save_students()
print("学员信息管理系统已退出!")
# 创建学生管理系统对象
manager = StudentManager()
# 添加学员
student1 = Student("张三", 18, "男")
manager.add_student(student1)
# 修改学员信息
manager.modify_student(student1, "李四", 20, "女")
# 查询学员信息
manager.query_student(student1)
# 显示所有学员信息
manager.show_all_students()
# 删除学员
manager.delete_student(student1)
# 退出系统
manager.exit_system()
```
阅读全文