''' # 1、练习上课作业讲解的面向对象代码 # 2、基于上课作业讲解的面向对象代码,扩写Student类 # 3、加入序列化与反序列化操作 # 4、对象之间的关联采用id号 # 5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象 ''' import pickle import uuid class School: def __init__(self, name, addr): self.name = name self.addr = addr self.classes = [] def save(self): with open(f'school_data/{self.name}.pick', 'wb') as f: pickle.dump(self, f) def select(self): with open(f'school_data/{self.name}.pick', 'rb') as f: school_obj = pickle.load(f) school_obj.tell_school() def related_class(self, class_id): self.classes.append(class_id) def tell_school(self): print(self.name.center(60, '=')) for class_id in self.classes: with open(f'class_data/{class_id}.pick', 'rb') as f: class_obj = pickle.load(f) class_obj.tell_class() class Class: def __init__(self, name): self.id = uuid.uuid4() self.name = name self.course = None self.students = [] def save(self): with open(f'class_data/{self.id}.pick', 'wb') as f: pickle.dump(self, f) def related_course(self, course_id): self.course = course_id def add_student(self, stu_id): self.students.append(stu_id) def tell_class(self): print(self.name, end=' ') with open(f'class_data/course_data/{self.course}.pick', 'rb') as course_f: course_obj = pickle.load(course_f) course_obj.tell_course() for stu_id in self.students: with open(f'class_data/student_data/{stu_id}.pick', 'rb') as stu_f: # 获取对应班级的学生信息 stu_obj = pickle.load(stu_f) stu_obj.tell_student() class Course: def __init__(self, name, time, price): self.id = uuid.uuid4() self.name = name self.time = time self.price = price def save(self): with open(f'class_data/course_data/{self.id}.pick', 'wb') as f: pickle.dump(self, f) def tell_course(self): print(f'<课程:{self.name} 课时:{self.time} 价格:{self.price}>') class Student: def __init__(self, name, age, sex): self.stu_id = uuid.uuid4() self.__name = name self.__age = age self.__sex = sex def save(self): with open(f'class_data/student_data/{self.stu_id}.pick', 'wb') as f: pickle.dump(self, f) def tell_student(self): print(f' 学生:{self.__name} <年龄:{self.__age} 性别:{self.__sex}>') # 创建学校对象 school_obj1 = School('Shanghai', '上海') school_obj2 = School('Beijing', '北京') # 创建班级对象 class_obj1 = Class('脱产14期') class_obj2 = Class('脱产15期') class_obj3 = Class('脱产29期') # 创建课程对象 course_obj1 = Course('python全栈开发', '6 months', 20000) course_obj2 = Course('linux运维', '5 months', 18000) # 将课程对象写入文件 course_obj1.save() course_obj2.save() # 创建学生对象 stu_obj1 = Student('莎莉娜', 18, '女') stu_obj2 = Student('小智', 18, '男') stu_obj3 = Student('小霞', 18, '女') stu_obj4 = Student('小瑶', 18, '女') # 将学生信息存入文件 stu_obj1.save() stu_obj2.save() stu_obj3.save() stu_obj4.save() # 班级关联课程 class_obj1.related_course(course_obj1.id) class_obj2.related_course(course_obj2.id) class_obj3.related_course(course_obj1.id) # 学生选择班级 class_obj1.add_student(stu_obj1.stu_id) class_obj1.add_student(stu_obj2.stu_id) class_obj2.add_student(stu_obj3.stu_id) class_obj3.add_student(stu_obj4.stu_id) # 将班级信息存入文件 class_obj1.save() class_obj2.save() class_obj3.save() # 讲班级id存入学校班级列表 school_obj1.related_class(class_obj1.id) school_obj1.related_class(class_obj2.id) school_obj2.related_class(class_obj3.id) # 讲学校对象存入文件 school_obj1.save() school_obj2.save() # 查看学校数据 school_obj1.select() school_obj2.select()