• 作业22


    作业22

    # 1、练习上课作业讲解的面向对象代码,明天默写
    # 2、基于上课作业讲解的面向对象代码,扩写Student类
    # 3、加入序列化与反序列化操作
    # 4、对象之间的关联采用id号
    # 5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象
    
    import pickle
    import uuid
    
    
    class School:
        name = "old boy"
    
        def __init__(self, name, adr):
            self.uid = uuid.uuid4()
            self.nickname = name
            self.adr = adr
            self.classes = []
    
        def show_school(self):
            print("{}({})".format(self.nickname, self.adr).center(50, "="))
            for class_uid in self.classes:
                with open("{}.pkl".format(class_uid), "rb") as f:
                    class_obj = pickle.load(f)
                class_obj.show_class()
                class_obj.show_students()
                print("-" * 50)
    
        def relate_class(self, class_obj):
            self.classes.append(class_obj.uid)
    
        def save_school(self):
            # print(self.__dict__)
            with open("{}.pkl".format(self.uid), "wb") as f:
                pickle.dump(self, f)
            with open("school","a",encoding="utf-8") as f:
                f.write("{%s:%s}
    " %(self.nickname,self.uid))
    
    
    
    class Class:
        def __init__(self, name):
            self.uid = uuid.uuid4()
            self.name = name
            self.course_uid = []
            self.students = []
    
        def show_class(self):
            print("{}".format(self.name), end="    ")
            for course_uid in self.course_uid:
                with open("{}.pkl".format(course_uid), "rb") as f:
                    course_obj = pickle.load(f)
                course_obj.show_course()
    
    
    
        def relate_course(self, course_obj):
            self.course_uid.append(course_obj.uid)
    
        def show_students(self):
            for stu_obj_uid in self.students:
                with open("{}.pkl".format(stu_obj_uid), "rb") as f:
                    stu_obj = pickle.load(f)
                stu_obj.show_stu()
    
    
        def save_class(self):
            with open("{}.pkl".format(self.uid), "wb") as f:
                pickle.dump(self, f)
            with open("class","a",encoding="utf-8") as f:
                f.write("{%s:%s}
    " %(self.name,self.uid))
    
    
    
    
    class Course:
        def __init__(self, name, period, price):
            self.uid = uuid.uuid4()
            self.name = name
            self.period = period
            self.price = price
    
        def show_course(self):
            print("[{}   {}  {}]".format(self.name, self.period, self.price))
    
        def save_course(self):
            with open("{}.pkl".format(self.uid), "wb") as f:
                pickle.dump(self, f)
            with open("course","a",encoding="utf-8") as f:
                f.write("{%s:%s}
    " %(self.name,self.uid))
    
    
    
    
    
    class Student:
        def __init__(self, name, age, num, gender):
            self.uid = uuid.uuid4()
            self.name = name
            self.age = age
            self.num = num
            self.gender = gender
    
        def relate_class(self, class_obj):
            self.class_obj = class_obj
            class_obj.students.append(self.uid)
    
        def show_stu(self):
            print("姓名:{}    年龄:{}    学号:{}    性别:{}".format(self.name, self.age, self.num, self.gender))
    
        def save_student(self):
            with open("{}.pkl".format(self.uid), "wb") as f:
                pickle.dump(self, f)
            with open("student", "a",encoding="utf-8") as f:
                f.write("{%s:%s}
    " % (self.name, self.uid))
    
    
    
    # # 创建新校区,班级,课程,学生
    # school_obj1 = School("老男孩魔都校区", "上海虹桥")
    # school_obj2 = School("老男孩帝都校区", "北京某某")
    # class_obj1 = Class("上海脱产14期")
    # class_obj2 = Class("上海脱产15期")
    # class_obj3 = Class("北京脱产29期")
    # course_obj1 = Course("python全栈开发", "5 mon", 22000)
    # course_obj2 = Course("linux 运维课程", "4 mon", 20000)
    # stu1 = Student("wu", 22, 1, "male")
    # stu2 = Student("egon", 24, 2, "female")
    # stu3 = Student("tank", 26, 3, "male")
    # stu4 = Student("ha", 28, 4, "female")
    #
    # # 校区关联班级
    # school_obj1.relate_class(class_obj1)
    # school_obj1.relate_class(class_obj2)
    # school_obj2.relate_class(class_obj3)
    # # 班级关联课程
    # class_obj1.relate_course(course_obj1)
    # class_obj2.relate_course(course_obj2)
    # class_obj3.relate_course(course_obj1)
    # # 学生选择班级
    # stu1.relate_class(class_obj1)
    # stu2.relate_class(class_obj2)
    # stu3.relate_class(class_obj2)
    # stu4.relate_class(class_obj3)
    #
    #
    # # 查看uuid
    # print(school_obj1.uid)
    # print(school_obj2.uid)
    # print(class_obj1.uid)
    # print(class_obj2.uid)
    # print(class_obj3.uid)
    # print(course_obj1.uid)
    # print(course_obj2.uid)
    # print(stu1.uid)
    # print(stu2.uid)
    # print(stu3.uid)
    # print(stu4.uid)
    #
    # # 保存信息
    # school_obj1.save_school()
    # school_obj2.save_school()
    # class_obj1.save_class()
    # class_obj2.save_class()
    # class_obj3.save_class()
    # course_obj1.save_course()
    # course_obj2.save_course()
    # stu1.save_student()
    # stu2.save_student()
    # stu3.save_student()
    # stu4.save_student()
    
    # # 输出信息
    # school_obj1.show_school()
    # school_obj2.show_school()
    # class_obj1.show_students()
    
    
    # 读取信息
    def obj_create(file):
        with open(file,"rb") as f:
            obj = pickle.load(f)
            return obj
    
    school_obj1 = obj_create("1151e0b9-8541-4917-8e12-3f817978ef0d.pkl")
    
    school_obj1.show_school()
    school_obj2 = obj_create("31fa891b-3f20-4e37-a1ad-f9157c46e246.pkl")
    school_obj2.show_school()
    

    1

  • 相关阅读:
    【NOIP模拟】排序
    【NOIP模拟】企鹅矩阵
    【NOIP模拟】花花森林
    【BZOJ1045】糖果传递
    【BZOJ1271】秦腾的教学评估
    【POJ3714】Raid
    【TYVJ1424】占卜DIY
    【POJ3190】The Pilots Brothers' refrigerator
    【HDU1055】Color a Tree
    【NOIP2012】国王游戏
  • 原文地址:https://www.cnblogs.com/achai222/p/12661857.html
Copyright © 2020-2023  润新知