pythonsuper的使用注意

来源:千锋教育
发布时间:2023-11-14 00:59:19
分享

python中super的使用注意

1、super()只能用于新式类中

所谓新式类,旧类的,关键就是看是不是有基类,有基类的就是形式类,比如classA(object),所以classA()自然就是旧式类了。

#单继承

classA(object):

def__init__(self,a,b):

self.a=a

self.b=b

defsayHello(self):

print('thisisclassA,a={},b={}'.format(self.a,self.b))

classB(A):

def__init__(self,a,b,c):

super(B,self).__init__(a,b)

self.c=c

defsayHello(self):

super(B,self).sayHello()

print('thisisbcall')

b=B('b','alsob','test')

b.sayHello()

#thisisclassA,a=b,b=alsob

#thisisbcall

2、super其实和父类没有实质性的关联

多重继承下,super就没有那么简单了。

#多重继承

classBase(object):

def__init__(self):

print('enterBase')

print('outBase')

classA(Base):

def__init__(self):

print('enterA')

super(A,self).__init__()

print('outA')

classB(Base):

def__init__(self):

print('enterB')

super(B,self).__init__()

print('outB')

classC(A,B):

def__init__(self):

print('enterC')

super(C,self).__init__()

print('outC')

c=C()

#enterC

#enterA

#enterB

#enterBase

#outBase

#outB

#outA

#outC

以上就是python中super的使用注意,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

声明:本站部分稿件版权来源于网络,如有侵犯版权,请及时联系我们。

相关推荐

  • pythongeth如何使用? python中geth如何使用?1、安装命令pipinstallpy-geth2、快速启动运行连接到mainnet的geth>>>fromgethimportLiveGethProcess>>>get
  • python用OpenCV在图像添加文本 python中用OpenCV在图像添加文本1、要将文本放入图像中,需要指定以下内容写入的文字数据放置它的位置坐标(即数据开始的左下角)。字体类型(检查cv.putText文档以获取受支持的字体)字体比
  • python使用OpenCV画线 python中使用OpenCV画线1、常见的参数img:您要绘制形状的图像color:形状的颜色。对于BGR,将其作为元组传递,例如:(255,0,0)对于蓝色。对于灰度,只需传递标量值即可。厚度:线
  • pythoncollections.Counter的计数 pythoncollections.Counter的计数1、说明Counter类型中的计数器还支持负值。通过简单的+-作为Counter类型对象的前缀,就可以实现正负计数过滤。Python的这一设计很
  • pythoncollections.Counter是什么? python中collections.Counter是什么?1、说明可以用来给可散列的对象计数,或者是当成多重集合来使用——多重集合就是集合里的元素可以出现多次1。类似于其它编程语言中的bags或者m
  • python如何安装pyenv python中如何安装pyenv1、安装pyenv系统Manjaro,可以直接yay安装:yay-Spyenv2、其他系统的可以使用aptsearch/yumsearch等看看软件包仓库有没有,有的话