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()
参数值
无参数