首页 > 代码库 > 【转载】python super的用法

【转载】python super的用法

转载地址:

http://blog.csdn.net/cxm19830125/article/details/20610533

 

 

 

super的用法是调用继承类的初始化方法,如下面的代码:

 1 class A(object): 2     def __init__(self): 3         print A __init__ 4         super(A, self).__init__() 5         print leave A 6          7 class C(object): 8     def __init__(self): 9         print C __init__10         super(C, self).__init__()11         print leave C12          13 class B(A,C):14     def __init__(self):15         print B __init__16         super(B, self).__init__()17         print leave B18  19 class D(B):20     def __init__(self):21         print D __init__22         super(D, self).__init__()23         print leave D24  25 if __name__ == __main__:26     D()

结果:

1 D __init__2 B __init__3 A __init__4 C __init__5 leave C6 leave A7 leave B8 leave D

 

【转载】python super的用法