比如有两个类A,类B,用类C把类A和类B组合起来,使得C有A和B的属性方法。
class Turtle:
def __init__(self,x):
self.num = x
class Fish:
def __init__(self,y):
self.num = y
class Pool:
def __init__(self,x,y):
self.turtle = Turtle(x)
self.fish = Fish(y)
def print_num(self):
print("水池里有乌龟 %d 只,鱼 %d 只!"%(self.turtle.num,self.fish.num))
pool = Pool(1,10)
pool.print_num()
组合结果
水池里有乌龟 1 只,鱼 10 只!
原文:https://www.cnblogs.com/ua-21/p/14801674.html