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