zoukankan      html  css  js  c++  java
  • Python学习第十七课——组合

    组合1

    #组合 1
    class Hand:
        pass
    
    
    class Foot:
        pass
    
    
    class Trunk:
        pass
    
    
    class Head:
        pass
    
    
    class Person:
        def __init__(self, id_num, name):
            self.id_num = id_num
            self.name = name
            self.hand = Hand()
            self.foot = Foot()
            self.trunk = Trunk()
            self.head = Head()
    
    p1=Person('111','join')
    
    print(p1.__dict__)

    组合2

    class School:
        def __init__(self, name, addr):
            self.name = name
            self.addr = addr
    
    
    class Course:
        def __init__(self, name, price, period, school):
            self.name = name
            self.price = price
            self.period = period
            self.school = school
    
    
    s1 = School('家里蹲', '郑州')
    s2 = School('市里蹲', '开封')
    
    c1 = Course('python', 10, '10天', s1)
    c2 = Course('python', 100, '100天', s2)
    # print(c1.__dict__)
    print(c1.school.name)  # 家里蹲
    print(c2.school.name)  # 市里蹲
    print(c1.school.addr)  # 郑州
    print(c2.school.addr)  # 开封

    组合例子之点课系统

    # 点课系统
    class School:
        def __init__(self, name, addr):
            self.name = name
            self.addr = addr
    
    
    class Teacher:     # 老师应该与课程进行关联
        def __init__(self, name, gongzi,course):
            self.name = name
            self.gongzi = gongzi
            self.course=course
    
    class Course:
        def __init__(self, name, price, period, school, teacher):  # 课程应该和学校 还有 老师进行关联
            self.name = name
            self.price = price
            self.period = period
            self.school = school
            self.teacher = teacher
    
    
    s1 = School('家里蹲', '郑州')
    s2 = School('市里蹲', '开封')
    s3 = School('县里蹲', '兰考')
    t1 = Teacher('憨憨', 10000, 'python')
    t2 = Teacher('玫玫', 1000, '安卓')
    t3 = Teacher('浩浩', 100, 'c')
    msg = '''
    1 家里蹲 郑州校区
    2 市里蹲 开封校区
    3 县里蹲 兰考校区
    '''
    msg1 = '''
    4 憨憨    python
    5 玫玫    安卓
    3 浩浩    c
    '''
    while True:
        print(msg)
        menu = {
            '1': s1,
            '2': s2,
            '3': s3
        }
        print(msg1)
        teacher_menu = {
            '4': t1,
            '5': t2,
            '6': t3,
        }
        choice = input('选择学校>>>')
        school_obj = menu[choice]  # 获取学校信息
    
        name = input('选择课程>>>')
        teacher_obj = teacher_menu[name]  # 获取老师信息
        gongzi = t1.gongzi
        price = input('选择费用>>>')
        period = input('选择周期>>>')
        teacher = input('选择老师>>>')
    
        new_course = Course(name, price, period, school_obj, teacher_obj)
    
        # print(new_teacher.__dict__)
        print('课程【%s】属于【%s】学校 老师为 %s' % (new_course.teacher.course, new_course.school.name, new_course.teacher.name))
        #课程【python】属于【家里蹲】学校 老师为 憨憨
  • 相关阅读:
    网页媒体播放利器
    iframe截取网站部分内容实现思路及代码
    网站如何添加访客统计代码
    Javascript写入txt和读取txt文件示例
    常见JS挂马方法及如何防止网站被黑客挂马?
    最新挂木马的10大操作方法
    Javascript教程:js异步模式编程的4种解决方法
    JavaScript对象属性的基础教程指南
    jsp中的JSTL与EL表达式用法
    Java 连接 SqlServer工具类
  • 原文地址:https://www.cnblogs.com/pyhan/p/12312093.html
Copyright © 2011-2022 走看看