0
使用超我得到一个错误的程序:混乱Python中
class TennisPlayer():
def __init__(self, rank):
self.rank = rank
def slogan(self):
print("What Slogan!")
class GrandSlamWinner(TennisPlayer):
def __init__(self, rank=0, slams=0):
TennisPlayer.__init__(self, rank)
self.slams = slams
def slogan(self):
super(GrandSlamWinner, self).slogan()
print("This: Are you serious!!")
if __name__ == "__main__":
mcenroe = GrandSlamWinner(1,7)
print(mcenroe.slams)
mcenroe.slogan()
我得到的错误:
Traceback (most recent call last):
File "subclassing.py", line 18, in <module>
mcenroe.slogan()
File "subclassing.py", line 12, in slogan
super(GrandSlamWinner, self).slogan()
TypeError: super() argument 1 must be type, not classobj
那么,我们怎么来覆盖Python的超类的方法呢?
此外,是否有任何标准的方法来修改或添加到超类而子类化,如一套约定 - 例如,在子类的初始化函数中使用超类吗?
干杯!
想必您使用的是Python 2.x,其中super不适用于旧式类。升级到3.x.并谷歌你的错误信息。 – jonrsharpe
你忘了从'TennisPlayer'中的'object'继承。 – user2357112
@ user2357112我读到这是隐含的。 – Rafael