zoukankan      html  css  js  c++  java
  • python每日作业4/8

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

    import uuid
    import pickle
    import os
    
    class School():
        def __init__(self,name,addr):
            self.name = name
            self.addr = addr
            self.classes_uid = []
            self.uid = uuid.uuid3(uuid.NAMESPACE_DNS, name)
    
        def add_class(self,class_obj):
            self.classes_uid.append(class_obj.uid)
    
        def show_school(self):
            print(self.name.center(50,'='))
            for class_file in self.classes_uid:
                if os.path.exists('{}.pkl'.format(class_file)):
                    with open('{}.pkl'.format(class_file),'rb') as f:
                        class1 = pickle.load(f)
                    class1.show_class()
    
        def save(self):
            with open('{}.pkl'.format(self.uid), 'wb') as f:
                pickle.dump(self, f)
            with open('uid','a',encoding='utf-8') as f:
                f.write('{}
    '.format(str({self.name:self.uid})))
    
    class Class():
        def __init__(self,name):
            self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
            self.name = name
            self.courses_uid = []
            self.students = []
        def add_course(self,course_obj):
            self.courses_uid.append(course_obj.uid)
        def show_class(self):
            print(self.name)
            for cou1 in self.courses_uid:
                if os.path.isfile('{}.pkl'.format(cou1)):
                    with open('{}.pkl'.format(cou1), 'rb') as f:
                        cou = pickle.load(f)
                    cou.show_course()
    
    
        def save(self):
            with open('{}.pkl'.format(self.uid), 'wb') as f:
                pickle.dump(self, f)
    
    
    class Course():
        def __init__(self,name,price,time):
            self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
            self.name = name
            self.price = price
            self.time = time
        def show_course(self):
            print('课程名称:{} 课程价格:{} 课程周期:{}'.format(self.name,self.price,self.time))
    
        def save(self):
            with open('{}.pkl'.format(self.uid), 'wb') as f:
                pickle.dump(self,f)
    
    class Student():
        def __init__(self,name,age,sex):
            self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
            self.name = name
            self.age = age
            self.sex = sex
            self.classes = []
            self.cores = None
        def choose_class(self,class_obj):
            self.classes.append(class_obj)
        def show_class(self):
            for i in self.classes:
                i.show_class()
    
        def save(self):
            with open('{}.pkl'.format(self.uid), 'wb') as f:
                pickle.dump(self, f)
    
    
    class Teacher():
        def __init__(self,name):
            self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
            self.name = name
        def set_core(self,stu_obj,core):
            str_obj.cores = core
    
        def save(self):
            with open('{}.pkl'.format(self.uid), 'wb') as f:
                pickle.dump(self, f)
    
    school_shanghai = School('魔都校区','上海')
    class1 = Class('上海14期')
    class2 = Class('上海15期')
    course1 = Course('python全栈',20000,'6 mons')
    course2 = Course('linux运维',18000,'6 mons')
    class1.add_course(course1)
    class2.add_course(course2)
    
    school_shanghai.add_class(class1)
    school_shanghai.add_class(class2)
    stu1=Student("egon",18,"man")
    stu1.choose_class(class1)
    
    
    course1.save()
    course2.save()
    class1.save()
    class2.save()
    stu1.save()
    school_shanghai.show_school()
    stu1.show_class()
    
    
    '''
    执行结果
    =======================魔都校区=======================
    上海14期
    课程名称:python全栈 课程价格:20000 课程周期:6 mons
    上海15期
    课程名称:linux运维 课程价格:18000 课程周期:6 mons
    上海14期
    课程名称:python全栈 课程价格:20000 课程周期:6 mons
    '''
  • 相关阅读:
    关于sqlite数据库在使用过程中应该注意以下几点
    关于THREAD线程中CurrentCulture与CurrentUICulture的学习
    转:ASP.NET MVC3升级到ASP.NET MVC4
    win8 iis安装及网站发布
    转: CKEditor/CKFinder升级心得
    [更新]Windows Phone 实现类似“微博”下拉刷新效果
    EntityFramework中使用Include可能带来的问题
    [更新]Luke.Net for Pangu 盘古分词版更新
    文件大小友好显示类
    找出最慢的查询语句Find your slowest queries
  • 原文地址:https://www.cnblogs.com/baicai37/p/12662404.html
Copyright © 2011-2022 走看看