python中使用super函數調用父類構造函數,具體方法如下:
1.首先,在python項目中,定義一個Adata類,并創建一個__init__構造函數;
class Adata
def __init__(self,num):
self,num = num
2.Adata類定義好后,在Adata類中定義一個showNum類方法,并打印self.num;
class Adata
def __init__(self,num):
self,num = num
def showNum(self):
print(self.num)
3.在定義一個Bdata類,并創建構造函數,傳入type和sun參數;
class Adata
def __init__(self,num):
self,num = num
def showNum(self):
print(self.num)
class Bdata:
def __init__(self,type,sun)
self.type = type
self.sum = sum
4.最后,定義一個Cdata類,繼承父類Adata和Bdata,使用super函數即可調用父類的構造函數;
class Adata
def __init__(self,num):
self,num = num
def showNum(self):
print(self.num)
class Bdata:
def __init__(self,type,sun)
self.type = type
self.sum = sum
class Cdata(Adata,Bdata):
def __init__(self,num,type,sun):
super.__init__(num)
super.(Cdata,self).__init__(num)
Bdata.__init__(self,type,sun)