Python 類的繼承詳解
Python既然是面向對象的,當然支持類的繼承,Python實現類的繼承比JavaScript簡單。
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")
調用
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 .....
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答