Python进阶-----类的组合

  定义一个人的类,人有头、躯干、手、脚等数据属性,这几个属性又可以是通过一个类实例化的对象,这就是组合

 1 class Hand:
 2     pass
 3 class Foot:
 4     pass
 5 class Trunk:
 6     pass
 7 class Head:
 8     pass
 9 
10 class Person:
11     def __init__(self,id_num,name):
12         self.id_num = id_num
13         self.name = name
14         self.hand = Hand()
15         self.foot = Foot()
16         self.trunk = Trunk()
17         self.head = Head()
18 #通过上例可以发现,可以将单独的类组合在一起放入到Person这个大类里
19 p = Person('6125221111111','Meanwey')
20 print(p.__dict__)

 

posted @ 2018-10-14 23:14  Meanwey  阅读(422)  评论(0编辑  收藏  举报