zoukankan      html  css  js  c++  java
  • emmm......就当练习了系列20

    1、练习上课作业讲解的面向对象代码,明天默写

    class School:
        school_name = 'OLDBOY'
    
        def __init__(self, nickname, addr):
            self.nickname = nickname
            self.addr = addr
            self.classes = []
    
        def related_class(self, class_obj):
            # self.classes.append(班级名字)
            # self.classes.append(class_name)
            self.classes.append(class_obj)
    
        def tell_class(self): #
            # 打印的班级的名字
            print(self.nickname.center(60,'='))
            # 打印班级开设的课程信息
            for class_obj in self.classes:
                class_obj.tell_course()
    class Class:
        def __init__(self, name):
            self.name = name
            self.course = None
            self.student = []
    
        def related_course(self, course_obj):
            # self.course = course_name
            self.course = course_obj
    
        def related_student(self,student_obj):
            self.student.append(student_obj)
    
        def tell_course(self):
            print('%s' % self.name,end=" ")
            self.course.tell_info() # 打印课程的详细信息
    class Course:
        def __init__(self,name,period,price):
            self.name=name
            self.period=period
            self.price=price
    
        def tell_info(self):
            print('<课程名:%s 周期:%s 价钱:%s>' %(self.name,self.period,self.price))

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

    class School:
        school_name = 'OLDBOY'
        addr = ''
        classes = []
        def related_school(self,nickname, addr):
            self.school_name = nickname
            self.addr = addr
    
        def related_class(self, class_obj):
            self.classes.append(class_obj)
    
        def tell_class(self): #
            print(self.school_name.center(60,'='))
            for class_obj in self.classes:
                print(class_obj.name)
    
    
    class Class(School):
        def __init__(self, name):
            self.name = name
            self.classes.append(self)
            self.course = []
            self.student = []
    
        def related_course(self, course_obj):
            # self.course = course_name
            self.course.append(course_obj)
    
        def related_student(self,student_obj):
            self.student.append(student_obj)
    
        def tell_course(self):
            print('%s课程' % self.name)
            for course_obj in self.course:
                course_obj.tell_info() # 打印课程的详细信息
    
        def tell_student(self):
            print('%s学生' % self.name)
            for student_obj in self.student:
                student_obj.tell_info()
    
    class Course:
        def __init__(self,name,time):
            self.name=name
            self.time=time
    
        def tell_info(self):
            print('<课程名:%s 课时:%s>' %(self.name,self.time))
    
    class Student:
        def __init__(self, name, sno):
            self.name = name
            self.sno = sno
    
        def tell_info(self):
            print('<学生名:%s 学号:%s >' % (self.name, self.sno,))
    
    
    class pickle_obj:
    
        def __init__(self, obj):
            import os
            self.obj = obj
            self.path = os.path.join(os.path.dirname(__file__), obj.name,'.pkl')
    
        def dumps(self):
            import pickle
            with open(self.path,'w',encoding='utf-8')as f:
                res = pickle.dump(self.obj, f)
            print(res)
    
        def loads(self):
            import pickle
            with open(self.path, 'r', encoding='utf-8')as f:
                res = pickle.load(f)
            print(res)
    
    class_obj1 = Class("py14")
    class_obj2 = Class("py15")
    class_obj3 = Class("py16")
    
    class_obj1.related_school("oldboy","上海")
    class_obj1.tell_class()
    
    course_obj1 = Course("爬虫","30h")
    course_obj2 = Course("SQL","16h")
    course_obj3 = Course("数据分析","30h")
    
    class_obj1.related_course(course_obj1)
    class_obj1.related_course(course_obj2)
    
    class_obj2.related_course(course_obj2)
    class_obj2.related_course(course_obj3)
    
    class_obj3.related_course(course_obj1)
    class_obj3.related_course(course_obj3)
    
    class_obj1.tell_course()
    class_obj2.tell_course()
    class_obj3.tell_course()
    
    class_obj1.related_student(Student("小一",'001'))
    class_obj1.related_student(Student("小一1",'011'))
    class_obj2.related_student(Student("小二",'002'))
    class_obj2.related_student(Student("小二2",'022'))
    class_obj3.related_student(Student("小三",'003'))
    class_obj3.related_student(Student("小三3",'033'))
    
    class_obj1.tell_student()
    class_obj2.tell_student()
    class_obj3.tell_student()
    
    pickle_obj(class_obj1).dumps()
    pickle_obj(class_obj1).loads()
  • 相关阅读:
    C++ 栈和队列
    epoll 实际使用
    js数组的操作
    nodejs 基本类型和语法
    epoll 简单介绍及例子
    epoll 应用
    linux 查看系统状态方法
    struct stat结构体的详解和用法
    Asp.Net_获取IP地址
    Asp.Net_的传值跟存储值操作
  • 原文地址:https://www.cnblogs.com/lucky-cat233/p/12664338.html
Copyright © 2011-2022 走看看