Python面向对象方法写一个学生管理系统
时间: 2023-11-17 19:29:36 浏览: 82
好的,下面是一个简单的学生管理系统的Python面向对象实现:
```python
class Student:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def get_all_students(self):
return self.students
def get_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
if __name__ == '__main__':
manager = StudentManager()
while True:
print("1. Add student")
print("2. Remove student")
print("3. Get all students")
print("4. Get student by name")
print("5. Quit")
choice = input("Enter choice: ")
if choice == '1':
name = input("Enter name: ")
age = int(input("Enter age: "))
score = int(input("Enter score: "))
student = Student(name, age, score)
manager.add_student(student)
elif choice == '2':
name = input("Enter name: ")
student = manager.get_student(name)
if student:
manager.remove_student(student)
else:
print("Student not found")
elif choice == '3':
students = manager.get_all_students()
for student in students:
print(student.name, student.age, student.score)
elif choice == '4':
name = input("Enter name: ")
student = manager.get_student(name)
if student:
print(student.name, student.age, student.score)
else:
print("Student not found")
elif choice == '5':
break
else:
print("Invalid choice")
```
这个程序包含了两个类,`Student` 和 `StudentManager`。`Student` 类表示一个学生,`StudentManager` 类表示学生管理系统。这个程序允许用户添加、删除、获取所有学生和获取指定学生的信息。
阅读全文