• 4.8作业


    2、基于上课作业讲解的面向对象代码,扩写Student类
    3、加入序列化与反序列化操作
    4、对象之间的关联采用id号
    5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象

    import pickle
    import uuid
    
    
    class School:
    
        school_name = 'OLDBOY'
    
        def __init__(self, nickname, addr):
            self.nickname = nickname
            self.addr = addr
            self.uuid = uuid.uuid4()
            self.classes = []
            self.save_info()
    
    
        def relate_class(self, class_obj):
            self.classes.append(class_obj.uuid)
            self.save_info()
    
    
        def save_info(self):
            with open(f'db\school_file\{self.uuid}.pkl', 'wb') as f:
                pickle.dump(self, f)
    
    
        def get_class(self):
            print(self.nickname.center(60, '='))
            # 打印班级开班的课程信息
            for class_uuid in self.classes:
                with open(f'db\class_file\{class_uuid}.pkl', 'rb') as f:
                   class_obj = pickle.load(f)
                   class_obj.get_course()
                   class_obj.get_student()
    
    
    class Class:
        def __init__(self, name):
            self.name = name
            self.course = None
            self.students = []
            self.uuid = uuid.uuid4()
            self.save_info()
    
        def relate_course(self, course_obj):
            self.course = course_obj.uuid
            self.save_info()
    
        def save_info(self):
            with open(f'db\class_file\{self.uuid}.pkl', 'wb') as f:
                pickle.dump(self, f)
    
        def get_course(self):
            print('%s' % self.name, end=" ")
            with open(f'db\course_file\{self.course}.pkl', 'rb') as f:
                course_obj = pickle.load(f)
                course_obj.course_info()
    
    
        def get_student(self):
            print('%s' % self.name, end=" ")
            for student_uuid in self.students:
                with open(f'db\student_file\{student_uuid}.pkl', 'rb') as f:
                    student_obj = pickle.load(f)
                    student_obj.student_info()
    
    
    class Course:
        def __init__(self, name, period, price):
            self.name = name
            self.period = period
            self.price = price
            self.uuid = uuid.uuid4()
            self.save_info()
    
        def course_info(self):
            print('<课程名:%s 时长:%s 价格:%s>' % (self.name, self.period, self.price))
    
        def save_info(self):
            with open(f'db\course_file\{self.uuid}.pkl', 'wb') as f:
                pickle.dump(self, f)
    
    
    
    class Student:
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
            self.uuid = uuid.uuid4()
            self.my_class = None
            self.my_course = None
            self.save_info()
    
        def student_info(self):
            print('<学生名字:%s 年龄:%s 性别:%s>' % (self.name, self.age, self.sex))
    
        def relate_class(self, class_obj):
            self.my_class = class_obj.uuid
            self.my_course = class_obj.uuid
            class_obj.students.append(self.uuid)
            self.save_info()
            class_obj.save_info()
    
        def save_info(self):
            with open(f'db\student_file\{self.uuid}.pkl', 'wb') as f:
                pickle.dump(self, f)
    

      

    # 创建校区
    school_obj1 = School('老男孩魔都校区', '上海')
    school_obj2 = School('老男孩帝都校区', '北京')
    
    # # 创建班级
    class_obj1 = Class('脱产14期')
    class_obj2 = Class('脱产15期')
    class_obj3 = Class('脱产29期')
    
    # 上海校区开了:脱产14期,上海校区开了脱产15期
    school_obj1.relate_class(class_obj1)
    school_obj1.relate_class(class_obj2)
    # 北京校区开了:脱产29期
    school_obj2.relate_class(class_obj3)
    
    # 1、创建课程
    course_obj1 = Course('python全栈开发', '6mons', 20000)
    course_obj2 = Course('linux运维', '5mons', 18000)
    # 3、为班级关联课程对象
    class_obj1.relate_course(course_obj1)
    class_obj2.relate_course(course_obj2)
    class_obj3.relate_course(course_obj1)
    
    stu_obj1 = Student('jack', 18, 'male')
    stu_obj2 = Student('egon', 18, 'female')
    stu_obj3 = Student('alex', 18, 'female')
    
    stu_obj1.relate_class(class_obj1)
    stu_obj2.relate_class(class_obj2)
    stu_obj3.relate_class(class_obj3)
    
    school_obj1.get_class()
    school_obj2.get_class()
    

      

  • 相关阅读:
    初始Openwrt
    Angular CLI 使用教程指南参考
    Visual Studio Code怎么在同一窗口打开多个项目文件?
    mysql查看数据表是否有重复数据
    Angular UI框架 Ng-alain @delon的脚手架的生成开发模板
    VS2010 WDK7.1.0 Win7_64
    Nodejs使用TLS
    python定时执行方法
    Visual Studio Code 使用Git进行版本控制
    VSCode中设置Python解释器和pylint的波浪线问题
  • 原文地址:https://www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12663198.html
Copyright © 2020-2023  润新知