class Students():
def __init__(self, id, name, age):
self.name = name
self.age = age
self.id = id
def __str__(self):
return f'学号:{self.id},名字:{self.name},年龄:{self.age}'
class StudentsList:
def __init__(self, capacity=10):
self.capacity = capacity
self.size = 0
self.data = {}
def add(self, student):
if len(self.data) >= self.capacity:
print('已满,无法添加学生')
return
if student.id in self.data:
print(f'{student.id}已添加')
else:
self.data[student.id] = student
self.size += 1
print(f'{student.name}已添加')
def delete(self, id):
if id in self.data:
del self.data[id]
self.size -= 1
print(f'{id}的学生已删除')
else:
print('查无此人')
def update(self, id, name=None, age=None):
if id in self.data:
s = self.data[id]
if name:
s.name = name
if age:
s.age = age
print(f'{id}学号的学生信息已更新')
else:
print('查无此人')
def list(self):
if not self.data:
print("学生列表为空!")
else:
print("当前列表:")
for student in self.data.values():
print(student)
student_list = StudentsList()
stu1 = Students(21001, '王五', 18)
stu2 = Students(21002, '马六', 20)
student_list.add(stu1)
student_list.add(stu2)
student_list.list()
student_list.delete(21001)
student_list.update(21002, '米老鼠', 22)
student_list.list()