Python类继承错误
代码如下:classBase:def_init_(self):self.data=[]defadd(self,x):self.data.append(x)classChi...
代码如下:
class Base:
def _init_(self):
self.data=[]
def add(self,x):
self.data.append(x)
class Child(Base):
def _init_(self):
self.data=[]
Base._init_(self)
def add(self,x):
Base.add(self,x)
def plus(self,a,b):
return a+b
oChild=Child()
oChild.add("str1")
print oChild.data
print oChild.plus(2,7)
错误如下:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "D:/PyCodes/test12", line 16, in <module>
oChild.add("str1")
File "D:/PyCodes/test12", line 11, in add
Base.add(self,x)
File "D:/PyCodes/test12", line 5, in add
self.data.append(x)
AttributeError: Child instance has no attribute 'data'
>>>
这个问题怎么解决? 展开
class Base:
def _init_(self):
self.data=[]
def add(self,x):
self.data.append(x)
class Child(Base):
def _init_(self):
self.data=[]
Base._init_(self)
def add(self,x):
Base.add(self,x)
def plus(self,a,b):
return a+b
oChild=Child()
oChild.add("str1")
print oChild.data
print oChild.plus(2,7)
错误如下:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "D:/PyCodes/test12", line 16, in <module>
oChild.add("str1")
File "D:/PyCodes/test12", line 11, in add
Base.add(self,x)
File "D:/PyCodes/test12", line 5, in add
self.data.append(x)
AttributeError: Child instance has no attribute 'data'
>>>
这个问题怎么解决? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询