继承问题
最后一排turtle和fish为什么都要加上.numclass 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)) turtle和fish只是一个对象,num是他们对应对象的属性 这和继承有什么关系 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))
用的数据都是实例化对象的属性,所以用.num调用实例化对象属性
页:
[1]