python用面向对象写学生管理系统
时间: 2023-07-18 10:27:29 浏览: 98
PYTHON面向对象之学生管理系统!
好的,我可以为您提供一个简单的Python学生管理系统的面向对象代码示例:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.student_list = []
def add_student(self, student):
self.student_list.append(student)
def remove_student(self, student):
self.student_list.remove(student)
def get_student(self, name):
for student in self.student_list:
if student.name == name:
return student
return None
def print_all_students(self):
for student in self.student_list:
print("Name: {}, Age: {}, Gender: {}, Grade: {}".format(student.name, student.age, student.gender, student.grade))
# 示例用法
sms = StudentManagementSystem()
# 添加学生
s1 = Student("Tom", 18, "male", "A")
sms.add_student(s1)
s2 = Student("Jane", 17, "female", "B")
sms.add_student(s2)
# 打印所有学生信息
sms.print_all_students()
# 查找学生
s3 = sms.get_student("Tom")
if s3:
print("Found student: {}, Age: {}, Gender: {}, Grade: {}".format(s3.name, s3.age, s3.gender, s3.grade))
else:
print("Student not found")
# 删除学生
sms.remove_student(s1)
sms.print_all_students()
```
这个学生管理系统包含两个类:一个是学生类,另一个是学生管理系统类。学生类用于存储学生信息,学生管理系统类用于添加、删除、查找和打印学生信息。这个系统可以通过实例化学生管理系统类,并通过调用其方法来进行操作。
阅读全文