类的组合用法
勤练多抄多写类的组合用法
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):
# 通过调用Turtle类、Fish类来形成组合
self.turtle = Turtle(x)
self.fish = Fish(y)
def printnum(self):
print('水池里共有%d只乌龟,%d条鱼' % (self.turtle.num, self.fish.num))
p = Pool(6, 8)
p.printnum()
页:
[1]