class Student():
def __init__(self,name,age,num,room):
self.__name = name
self.__age = age
self.__num = num
self.__room = room
@property
def name(self):
return self.__name
@property
def age(self):
return self.__age
@property
def num(self):
return self.__num
@property
def room(self):
return self.__room
def __str__(self):
return f'姓名{self.__name},年龄{self.__age},学号{self.__num},班级{self.__room}'
class Teacher():
def __init__(self,stu_list):
self.stu_list = stu_list
def show(self):
for stu in self.stu_list:
print(stu)
def add(self,new_stu):
self.stu_list.append(new_stu)
stu1 = Student('张三',18,10021,'a19c11')
stu2 = Student('李四',17,10022,'a19c11')
stu3 = Student('老五',16,10023,'a19c11')
stu4 = Student('老王',19,10024,'a19c11')
print(stu1.name)
print('=======')
print(stu1.name,stu1.age,stu1.num,stu1.room)
print('========')
clss = Teacher([stu1,stu2,stu3,stu4])
clss.show()
print('=====')
clss.add(Student('老思王',20,10025,'a19c11'))
clss.show()
python班级类的小练习
最新推荐文章于 2023-02-08 22:05:12 发布