作业4月9号
作业二:在昨天作业的基础之上
# 1、引入属性访问控制+property
# 2、引入继承与派生的概念来减少代码冗余
注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承
import uuid import pickle class Father: def __init__(self,name): self.name = name self.uid = str(uuid.uuid4()) self.lis = [] @property def save(self): with open(f'{self.uid}.pkl', 'wb')as f: pickle.dump(self, f) @property def get(self): with open(f'{self.uid}.pkl', 'rb')as f: res = pickle.load(f) print(res.__dict__) def add(self,id): self.lis.append(id) class School(Father): school_name = 'OLDBOY' def __init__(self, name,address): Father.__init__(self,name) self.address = address @property def tell_school_info(self): print(self.name.center(60, '=')) for id in self.lis: with open(f'{id}.pkl', 'rb')as f: res = pickle.load(f) res.tell_class_info class Class(Father): def __init__(self, name): Father.__init__(self,name) @property def tell_class_info(self): print(self.name.center(60, '=')) for id in self.lis: with open(f'{id}.pkl', 'rb')as f: res = pickle.load(f) res.tell_course_info class Course(Father): def __init__(self, name, cycle, price): Father.__init__(self,name) self.cycle = cycle self.price = price @property def tell_course_info(self): print('课程名称:%s 课程周期:%s 课程价格:%s' % (self.name, self.cycle, self.price)) class People: def __init__(self, name, age,sex): self.name = name self.age = age self.sex = sex self.uid = str(uuid.uuid4()) class Student(People,Father): def __init__(self,name,age,sex): People.__init__(self,name,age,sex) self.score = 0 self.course_uid = None def choice(self, course_uid): self.course_uid = course_uid def tell_student(self): print('id:%s 姓名:%s 年龄:%s 性别:%s 分数:%s ' % (self.uid, self.name, self.age, self.sex, self.score), end='') class Teacher(People,Father): def __init__(self, name, age, sex,salary, leve): People.__init__(self,name,age,sex) self.salary = salary self.leve = leve def score(self, student_obj, grade): student_obj.score = grade def tell_teacher(self): print('姓名:%s 年龄:%s 薪资:%s 等级:%s ' % (self.name, self.age, self.salary, self.leve), end='') # 创建学校对象 school_obj = School('老男孩魔都校区', '上海') # 创建班级对象 class_obj = Class('14期') class_obj2 = Class('15期') # 创建课程对象 course_obj = Course('python开发', '5个月', 1000) course_obj2 = Course('luinex开发', '1个月', 10000) # 将班级对象的uid存到学校对象中 school_obj.add(class_obj.uid) school_obj.add(class_obj2.uid) # 将课程对象的uid存到班级对象中 class_obj.add(course_obj.uid) class_obj.add(course_obj2.uid) class_obj2.add(course_obj.uid) class_obj2.add(course_obj2.uid) # 保存对象 school_obj.save school_obj.save class_obj.save class_obj2.save course_obj.save course_obj2.save # 打印数据 school_obj.tell_school_info # 从文件中根据uid去出数据 school_obj.get school_obj.get class_obj.get class_obj2.get course_obj.get course_obj2.get # 创建学生对象 stu1 = Student('张三', 18, '男') # 选课 stu1.choice(course_obj.uid) teach1 = Teacher('李四', 18, '男',20000, '特级教师') teach1.score(stu1, 90) stu1.save teach1.save stu1.tell_student()