作业4月8号
1、练习上课作业讲解的面向对象代码,明天默写
2、基于上课作业讲解的面向对象代码,扩写Student类
3、加入序列化与反序列化操作
4、对象之间的关联采用id号
5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
class School: school_name = 'OLDBOY' def __init__(self,nickname,addr): self.nickname =nickname self.addr = addr self.classes=[] def related_class(self,class_obj): self.classes.append(class_obj) def tell_class(self): print(self.nickname.center(60,'=')) for class_obj in self.classes: class_obj.tell_course() school_obj1=School('好男孩上海校区','上海') school_obj2=School('老男孩北京校区','北京') class Class: def __init__(self,name): self.name = name self.course = None def related_course(self,course_obj): self.course = course_obj def tell_course(self): print('%s'%self.name,end='') self.course.tell_info() class_obj1 = Class('脱产14期') class_obj2 = Class('脱产15期') class_obj3 = Class('脱产29期') school_obj1.related_class(class_obj1) school_obj1.related_class(class_obj2) school_obj2.related_class(class_obj3) class Course: def __init__(self,name,period,price): self.name=name self.period = period self.price = price def tell_info (self): print('课程名:%s 周期:%s 价格%s' %(self.name,self.period,self.price)) course_obj1= Course('python','6mons','20000') course_obj2 = Course('linux','5mons','10000') class_obj1.related_course(course_obj1) class_obj2.related_course(course_obj2) class_obj3.related_course(course_obj1) school_obj1.tell_class() school_obj2.tell_class()