新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python支持多繼承嗎
多繼承

單繼承有時(shí)候可能滿足不了我們所需的所以我們就會(huì)遇到多繼承,這個(gè)同樣能夠展示出代碼的重用。
同樣是上邊的例子,child不僅僅是繼承來(lái)自父親,還繼承來(lái)自母親。所以我們創(chuàng)建mother類
class Mother(object):
def __init__(self,face):
self.face=face
print('face',face)
def play(self):
print('mother go shopping with me')mothe類創(chuàng)建的屬性為face,其次我們還定義的一個(gè)相同的方法play 是為了展示多繼承中如果有相同的函數(shù)會(huì)調(diào)用哪個(gè)。
然后我們重寫(xiě)一下child類
from Father import Father from Mother import Mother class Child(Mother,Father): def __init__(self,money,face): Father.__init__(self, money) Mother.__init__(self,face)
python學(xué)習(xí)網(wǎng),免費(fèi)的python學(xué)習(xí)網(wǎng)站,歡迎在線學(xué)習(xí)!
當(dāng)前名稱:創(chuàng)新互聯(lián)Python教程:python支持多繼承嗎
本文網(wǎng)址:http://www.fisionsoft.com.cn/article/codshdp.html


咨詢
建站咨詢
