编写一个学生类student,包含的属性有学号、姓名、性别、年龄等,将所有学生存储 在一个数组中,编写学生管理程序实现如下操作: (1)增加一个学生; (2)根据学号删除某个学生; (3)将所有学生年龄增加一岁; (4)按数组中顺序显示所有学生信息;
时间: 2023-05-30 13:02:29 浏览: 74
class Student:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def increase_age(self):
for student in self.students:
student.age += 1
def display_students(self):
for student in self.students:
print("ID: {}, Name: {}, Gender: {}, Age: {}".format(student.id, student.name, student.gender, student.age))
# 测试
manager = StudentManager()
# 增加学生
manager.add_student(Student(1, "Tom", "Male", 18))
manager.add_student(Student(2, "Jack", "Male", 19))
manager.add_student(Student(3, "Lily", "Female", 20))
# 显示学生信息
manager.display_students()
# 删除学生
manager.remove_student(2)
# 显示学生信息
manager.display_students()
# 增加年龄
manager.increase_age()
# 显示学生信息
manager.display_students()
阅读全文