在Python中使用Super的困惑

时间:2017-09-22 20:29:37

标签: python class oop inheritance python-2.x

我收到了该程序的错误:

 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中覆盖超类方法?

此外,是否有任何标准方法可以在子类化时修改或添加超类,例如设置约定 - 例如nitialize子类的init函数中的超类?

干杯!

0 个答案:

没有答案