zoukankan      html  css  js  c++  java
  • python super()

    class Base(object):
        pass
    
    class Inject(Base):
        def __init__(self):
            super().__init__()                  # python3
            # super(Inject, self).__init__()    # python2
            pass
    
    class Child(Base):                  
        def __init__(self, user):
            super().__init__()                  # 1 python3 super() lets you avoid referring to the base class explicitly
            # super(Child, self).__init()       # 2 python2
            # Base.__init__(self)               # 3 avoid this, it limit you use multiple inheritance
            self.user = user
    
    class SuperInjector(Child, Inject):
        pass
        # if Child() use SuperInjector() # 2  can call Inject()
        # if Child() SuperInjector() use # 3 can't call Inject()
    
    
    
    # conclusion:Always use super to reference the parent class !!!
    
    # super() issue. In a class hierarchy with single inheritance, super() can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable; support cooperative multiple inheritance in a dynamic execution environment.   addr:https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods
    # standard doc on super. difference between 1 and 2.    addr:https://stackoverflow.com/questions/222877/what-does-super-do-in-python
    # method resolution order(MRO)   addr:https://stackoverflow.com/questions/2010692/what-does-mro-do
    # difference between 2 and 3.   addr:https://stackoverflow.com/questions/222877/what-does-super-do-in-python/33469090#33469090
  • 相关阅读:
    继承
    JAVA接口的继承与集合
    JAVA接口
    c++程序—敲桌子
    c++程序—水仙花数
    c++程序—while猜数字游戏
    c++程序—switch分支
    c++程序—三目运算符
    c++程序—if语句实践
    c++程序—选择结构
  • 原文地址:https://www.cnblogs.com/vickey-wu/p/7908601.html
Copyright © 2011-2022 走看看