类的组合用法
1 class Turtle: 2 def __init__(self, x): 3 self.num = x 4 5 6 class Fish: 7 def __init__(self, y): 8 self.num = y 9 10 11 class Pool: 12 def __init__(self, x, y): 13 # 通过调用Turtle类、Fish类来形成组合 14 self.turtle = Turtle(x) 15 self.fish = Fish(y) 16 17 def printnum(self): 18 print('水池里共有%d只乌龟,%d条鱼' % (self.turtle.num, self.fish.num)) 19 20 21 p = Pool(6, 8) 22 p.printnum()