【03】类——拾遗

类的组合

比如有两个类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 只!
posted @ 2021-05-23 17:58  qwaszx1!  阅读(51)  评论(0)    收藏  举报
HTML