zoukankan      html  css  js  c++  java
  • python学习-63 组合

                                                                组合

    1.什么是组合?

    定义一个类,由数据属性构成,这几个属性又可以是通过一个类实例化的对象,这就是组合。

    举例:

    class School:
        def __init__(self,name,address):
            self.name = name
            self.address = address
        def introduce(self):
            print('我们学校是好学校')
    class Course:
        def __init__(self,name,price,period,school):
            self.name =name
            self.price =price
            self.period =period
            self.school =school
    
    s1 = School('qinghua','beijing')
    s2 = School('qinghua','nanjing')
    s3 = School('qinghua','hebei')
    
    c1 = Course('linux',1000,'1h',s1)
    
    print(c1.__dict__)                 # 查看信息
    print(c1.school.name)

    运行结果:

    {'name': 'linux', 'price': 1000, 'period': '1h', 'school': <__main__.School object at 0x0056DE50>}
    qinghua
    
    Process finished with exit code 0
  • 相关阅读:
    vue中的ref,refs使用
    setTimeout 为0的作用
    click 和 mousedown 以及map地图的pointerdown
    electron图标
    websocket
    居中
    一键生成vue模板
    IntelliJ IDEA 安装破解及汉化详解
    基础的一些东西
    git 合作开发
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11765086.html
Copyright © 2011-2022 走看看