我刚刚开始学习Python,我不太明白这段代码中的问题在哪里。我有一个基类Proband有两个方法,我想创建一个子类Gesunder,我想覆盖属性idn,artefakte。
import scipy.io
class Proband:
def __init__(self,idn,artefakte):
self.__idn = idn
self.artefakte = artefakte
def getData(self):
path = 'C:\matlab\EKGnurbild_von Proband'+ str(self.idn)
return scipy.io.loadmat(path)
def __eq__(self,neueProband):
return self.idn == neueProband.idn and self.artefakte == neueProband.artefakte
class Gesunder(Proband):
def __init__(self,idn,artefakte,sportler):
super().__init__(self,idn,artefakte)
self.__sportler = sportler
hans = Gesunder(2,3,3)
答案 0 :(得分:2)
您的代码中有2个问题。在python 2中:
super()
有两个参数:类名和实例super()
,基类必须继承自object
所以你的代码变成了:
import scipy.io
class Proband(object):
def __init__(self,idn,artefakte):
self.__idn = idn
self.artefakte = artefakte
def getData(self):
path = 'C:\matlab\EKGnurbild_von Proband'+ str(self.idn)
return scipy.io.loadmat(path)
def __eq__(self,neueProband):
return self.idn == neueProband.idn and self.artefakte == neueProband.artefakte
class Gesunder(Proband):
def __init__(self,idn,artefakte,sportler):
super(Gesunder, self).__init__(idn,artefakte)
self.__sportler = sportler
hans = Gesunder(2,3,3)
请注意,对super(Gesunder, self).__init__
的调用没有self
作为第一个参数。
答案 1 :(得分:0)
在Python 2中,super()
本身无效。相反,您必须使用super(ClassName, self)
。
super(Gesunder, self).__init__(self, idn, artefakte)
答案 2 :(得分:0)
super()
来电应修改为:
super(Gesunder, self).__init__(self, idn, artefakte)