python子类调用父类方法在回到子类方法

1.子类调用父类:
super(subClass,subObject).fatherMethod

super(testAdmin, test).doSomethings()

2.父类中调用子类方法:

 child_method = getattr(self, 'doSomethings')  # 获取子类的out()方法
 child_method()

3.example:
父类

class IAdmin:
    adminCount = 0

    def __init__(self):
        pass

    def doSomethings(self):

        child_method = getattr(self, 'doSomethings')  # 获取子类的out()方法
        child_method()

    def doAdmin(self):
        print("this is father IAdmin Method")
        self.doSomethings()

子类


from Interface.common.IAdmin import IAdmin


class testAdmin(IAdmin):
    def __init__(self):
        pass

    def doSomethings(self):
        print("this is subClass Method")


test = testAdmin()
super(testAdmin, test).doAdmin()

结果:

this is father IAdmin Method
this is subClass Method
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容