Python super() 函式
示例
建立一個類,它將繼承另一個類的所有方法和屬性
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x = Child("Hello, and welcome!")
x.printmessage()
自己動手試一試 »
定義和用法
super()
函式用於訪問父類或兄弟類的方法和屬性。
super()
函式返回一個代表父類的物件。
語法
super()
引數值
無引數