导语:
本文主要介绍了关于python中super的使用注意的相关知识,希望可以帮到处于编程学习途中的小伙伴
1、super()只能用于新式类中
所谓新式类和旧式类,关键是看有没有基类,是正规类,比如class A(object),那么class A()自然是一个old-样式类。
# 单继承
class A(object):
def __init__(self, a, b):
self.a = a
self.b = b
def sayHello(self):
print('this is class A, a={},b={}'.format(self.a, self.b))
class B(A):
def __init__(self, a, b, c):
super(B, self).__init__(a,b)
self.c = c
def sayHello(self):
super(B, self).sayHello()
print('this is b call')
b = B('b','also b','test')
b.sayHello()
# this is class A, a=b,b=also b
# this is b call
2、super 其实和父类没有实质性的关联
多重继承下,super就没有那么简单了。
# 多重继承
class Base(object):
def __init__(self):
print('enter Base')
print('out Base')
class A(Base):
def __init__(self):
print('enter A')
super(A, self).__init__()
print('out A')
class B(Base):
def __init__(self):
print('enter B')
super(B, self).__init__()
print('out B')
class C(A, B):
def __init__(self):
print('enter C')
super(C, self).__init__()
print('out C')
c = C()
#enter C
#enter A
#enter B
#enter Base
#out Base
#out B
#out A
#out C
以上就是python中super的使用注意,希望能对大家有所帮助。
更多Python学习指路:
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Python继承原理分析11/27
- ♥ python帮助功能有什么用?11/06
- ♥ 完整的解释! python中的符号用法以及如何使用它08/24
- ♥ python3中的cached_property函数是什么?01/04
- ♥ python如何读取图像11/10
- ♥ 如何在kali中安装python08/27
内容反馈