zoukankan      html  css  js  c++  java
  • python类中super()和__init__()的区别

    class Base(object):     def __init__(self):        

    print 'Base create'

    class childB(Base):    

    def __init__(self):        

    print 'creat B ',        

    super(childB, self).__init__()

    class childA(childB,Base):    

    def __init__(self):        

    print 'creat A ',        

    Base.__init__(self)

    if __name__=="__main__":

    childA()

    结果:creat A  Base create

    class Base(object):    

    def __init__(self):        

    print 'Base create'

    class childA(Base):    

    def __init__(self):        

    print 'creat A ',        

    Base.__init__(self)

    class childB(childA,Base):    

    def __init__(self):        

    print 'creat B ',        

    super(childB, self).__init__()

    if __name__=="__main__":    

    childB()

    结果:creat B  creat A  Base create

    class Base():    

    def __init__(self):        

    print 'Base create'

    class childA(Base):    

    def __init__(self):        

    print 'creat A ',        

    Base.__init__(self)

    if __name__=="__main__":
        childA()

    结果:creat A  Base create

    class Base():    

    def __init__(self):        

    print 'Base create'

    class childA(Base):    

    def __init__(self):        

    print 'creat B ',        

    super(childA, self).__init__()

    if __name__=="__main__":     

    childA()

    结果:

    creat B
    Traceback (most recent call last):
      File "D:eclipse est est1.py", line 17, in <module>
        childA()
      File "D:eclipse est est1.py", line 14, in __init__
        super(childA, self).__init__()
    TypeError: super() argument 1 must be type, not classobj

  • 相关阅读:
    200. Number of Islands
    [Leetcode] 70. Climbing Stairs Java
    LeetCode 64. Minimum Path Sum Java
    LeetCode 63. Unique Paths II Java
    LeetCode 62. Unique Paths Java
    [LeetCode 241] Different Ways to Add Parentheses Java
    LeetCode 240. Search a 2D Matrix II Java
    LeetCode 215. Kth Largest Element in an Array Java
    LeetCode 169. Majority Element Java
    LeetCode 53. Maximum Subarray Java
  • 原文地址:https://www.cnblogs.com/xianhaiyan/p/5981388.html
Copyright © 2011-2022 走看看