新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中super的使用注意
1、SUPER()只能用于新式類中

所謂新式類,舊類的,關(guān)鍵就是看是不是有基類,有基類的就是形式類,比如class A(object),所以class A()自然就是舊式類了。
# 單繼承
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 call2、super 其實(shí)和父類沒有實(shí)質(zhì)性的關(guān)聯(lián)
多重繼承下,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學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:python中super的使用注意
網(wǎng)站路徑:http://fisionsoft.com.cn/article/dhhpjpc.html


咨詢
建站咨詢
