我是蟒蛇初学者,我不懂继承和 __init__()
。
class Num:
def __init__(self,num):
self.n1 = num
class Num2(Num):
def show(self):
print self.n1
mynumber = Num2(8)
mynumber.show()
结果: 8
这是可以的。但是我将 Num2
替换为
class Num2(Num):
def __init__(self,num):
self.n2 = num*2
def show(self):
print self.n1,self.n2
结果: Error. Num2 has no attribute "n1".
In this case, how can Num2
access n1
?