【问题标题】:Calling a superclass method from a subclass in python从python中的子类调用超类方法
【发布时间】:2013-05-23 01:40:53
【问题描述】:

我想知道子类是否可以调用其父静态方法,并对其进行了测试:它的工作方式与我希望的一样!

class A(object):
    @classmethod
    def static(cls):
    print('act on '+cls.__name__)

class B(A):
    def foo(self):
        print('foo()')

>>> B.static()
act on B

我想知道在使用该技术时是否有需要注意的陷阱......

有什么建议吗?

【问题讨论】:

    标签: python call subclass static-methods superclass


    【解决方案1】:

    您调用的是父母的classmethod,而不是staticmethod

    不管怎样,它们都可以用子类调用。

    如果您在B 中覆盖Aclassmethod,您仍然可以使用super(B, cls).static 引用A.static

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 2011-10-24
      • 2012-04-18
      • 1970-01-01
      相关资源
      最近更新 更多