Python 類的繼承實(shí)例詳解
更新時(shí)間:2017年03月25日 14:23:16 投稿:lqh
這篇文章主要介紹了Python 類的繼承實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
Python 類的繼承詳解
Python既然是面向?qū)ο蟮?,?dāng)然支持類的繼承,Python實(shí)現(xiàn)類的繼承比JavaScript簡(jiǎn)單。
Parent類:
class Parent: parentAttr = 100 def __init__(self): print("parent Init") def parentMethod(self): print("parentMethod") def setAttr(self,attr): self.parentAttr = attr def getAttr(self): print("ParentAttr:",Parent.parentAttr)
Child類
class Child(Parent): def __init__(self): print("child init") def childMethod(self): print("childMethod")
調(diào)用
p1 = Parent(); p1.parentMethod(); c1 = Child(); c1.childMethod();
輸出:
parent Init parentMethod child init childMethod Press any key to continue . . .
Python支持多繼承
class A: # 定義類 A ..... class B: # 定義類 B ..... class C(A, B): # 繼承類 A 和 B .....
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Python for循環(huán)搭配else常見問題解決
這篇文章主要介紹了Python for循環(huán)搭配else常見問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02VSCode中autopep8無法運(yùn)行問題解決方案(提示Error: Command failed,usage)
這篇文章主要介紹了VSCode中autopep8無法運(yùn)行問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03對(duì)python讀取CT醫(yī)學(xué)圖像的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python讀取CT醫(yī)學(xué)圖像的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01