• python学习之面向对象一作业


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

    class School():
    
        school_name = 'Oldboy'
        def __init__(self,nick_name,address):
            self.nick_name = nick_name
            self.address = address
            self.classes = []
    
        def related_class(self,class_obj):
            self.classes.append(class_obj)
    
        def tell_info(self):
            print('='*22+' %s '%self.nick_name+'='*22)
            for class_obj in self.classes:
                class_obj.tell_info()
    
    class Class():
    
        def __init__(self,name):
            self.name = name
            self.course = None
    
        def related_course(self,course_obj):
            self.course = course_obj
    
        def tell_info(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))
    
    
    
    class Student():
    
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex
            self.course = []
            self.class_name = None
    
        def related_course(self,course_name):
            self.course.append(course_name)
        def related_class(self,class_name):
            self.class_name = class_name
    
        def tell_info(self):
            print('='*20+' 学生信息 '+'='*20)
            print('姓名:%s 年龄:%s 性别:%s 班级:%s'%(
                self.name,self.age,self.sex,self.class_name
            ))
            print('课程信息:')
            for course in self.course:
                course.tell_info()
    
    cour1 = Course('python开发','6 months','20000')
    cour2 = Course('linux运维','6 months','18000')
    
    class1 = Class('python14期')
    class1.related_course(cour1)
    class2 = Class('python 9期')
    class2.related_course(cour1)
    class3 = Class('linux 25期')
    class3.related_course(cour2)
    
    sch1 = School('老男孩魔都校区','上海')
    sch1.related_class(class1)
    sch1.related_class(class3)
    sch2 = School('老男孩帝都校区','北京')
    sch2.related_class(class2)
    
    stu1 = Student('vincent','18','male')
    stu1.related_class(class1.name)
    stu1.related_course(cour1)
    stu1.related_course(cour2)
    stu1.tell_info()
    
  • 相关阅读:
    JavaWeb 期末设计 测试部署
    JavaWeb 期末项目设计 业务逻辑与实现
    JavaEE期末设计-银行转账系统需求文档分析
    Lamda 表达式
    Spring AOP capabilities and goals
    LDAP & Implement
    RESTful levels & HATEOAS
    Servlet CDI
    spring 历史&哲学
    mysql backup & recovery
  • 原文地址:https://www.cnblogs.com/leilijian/p/12664213.html
Copyright © 2020-2023  润新知