zoukankan      html  css  js  c++  java
  • 面向对象【五】类的组合

    python之类的组合

     

    类的组合

    学校与课程没有共同点,课程与老师没有共同点,但是学校与课程有关联,课程与老师有关联;学校、课程、老师是三个完全不同的类;课程是属于学校的,老师是教课程的,此时我们就用到类的组合来关联,学校-课程,课程-老师;

    复制代码
    class School:
        """
        学校类
        """
        def __init__(self,name,address):
            self.name = name
            self.address = address
    class Course:
        """
        课程类
        """
        def __init__(self,name,price,period,school):
            self.name = name
            self.price = price
            self.period = period
            self.school = school
    class Teacher:
        """
        老师类
        """
        def __init__(self,name,gender,age,course):
            self.name = name
            self.gender = gender
            self.age = age
            self.course = course
    #学校实例化
    x1 = School("oldboy","北京校区")
    x2 = School("oldboy","天津校区")
    x3 = School("oldboy","南京校区")
    #课程实例化
    k1 = Course("python",19880,"6个月",x1) #课程与学校关联
    k2 = Course("linux",17880,"4个月",x2)
    k3 = Course("go",20000,"8个月",x3)
    #教师实例化
    j1 = Teacher("alex","male",34,k2) #教师与课程关联
    j2 = Teacher("武sir","male",30,k1)
    j3 = Teacher("林海峰","male",35,k3)
    print(k1.__dict__)
    print(k1.school.name)
    print(j1.__dict__)
    print(j1.course.name)
    复制代码
    class School:
        """
        学校类
        """
        def __init__(self,name,address):
            self.name = name
            self.address = address
    class Course:
        """
        课程类
        """
        def __init__(self,name,price,period,school):
            self.name = name
            self.price = price
            self.period = period
            self.school = school
    class Teacher:
        """
        老师类
        """
        def __init__(self,name,gender,age,course):
            self.name = name
            self.gender = gender
            self.age = age
            self.course = course
    #学校实例化
    x1 = School("oldboy","北京校区")
    x2 = School("oldboy","天津校区")
    x3 = School("oldboy","南京校区")
    #课程实例化
    k1 = Course("python",19880,"6个月",x1) #课程与学校关联
    k2 = Course("linux",17880,"4个月",x2)
    k3 = Course("go",20000,"8个月",x3)
    #教师实例化
    j1 = Teacher("alex","male",34,k2) #教师与课程关联
    j2 = Teacher("武sir","male",30,k1)
    j3 = Teacher("林海峰","male",35,k3)
    print(k1.__dict__)
    print(k1.school.name)
    print(j1.__dict__)
    print(j1.course.name)
    
    msg = """
    oldboy 北京校区
    oldboy 天津校区
    oldboy 南京校区
    """
    res = """
    python
    linux
    go
    """
    while True:
        print(msg)
        menu = {
            "1":x1,
            "2":x2,
            "3":x3,
        }
        choice = input("选择学校>>>:")
        school_boj = menu[choice]
        name = input("课程名称>>>:")
        price = input("课程价钱>>>:")
        period = input("课程周期>>>:")
        new_course = Course(name,price,period,school_boj)
        print("课程【%s】属于【%s】学校"%(new_course.name,new_course.school.name))
        print(res)
        inner = {
            "1":k1,
            "2":k2,
            "3":k3,
    
        }
        choice1 = input("选择课程>>>:")
        course_boj = inner[choice1]
        name = input("老师名称>>>:")
        gender = input("老师性别>>>:")
        age = input("老师年龄>>>:")
        new_teacher = Teacher(name,gender,age,course_boj)
        print("老师【%s】教的是【%s】"%(new_teacher.name,new_teacher.course.name))
    
    选课系统
    选课系统
  • 相关阅读:
    如何使用pip安装PythonMySQLdb模块?
    Linux:信号(1):signal函数、pause函数、alarm函数
    python字符串前面加上'r'的作用
    在LINUX中 用Ctrl+z挂起的命令怎么切回到原任务的命令窗口
    A*寻路初探 GameDev.net
    [3d跑酷] Xcode5 打包 发布配置
    [cb]NGUI组件基类之 UIWidget
    [cb]Unity 项目架构
    使用Unity3D的50个技巧:Unity3D最佳实践
    Doxygen Tool For Unity
  • 原文地址:https://www.cnblogs.com/youxiu123/p/11481120.html
Copyright © 2011-2022 走看看