1. 程式人生 > >Python:類的組合

Python:類的組合

class Turtle:
    def __init__(self,x):
        self.num=x

class Fish:
    def __init__(self,x):
        self.num=x

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) print(pool.print_num())