继承:将父类__init__参数创建为子类
问题描述:
当我实例化一个Child
类时,如何才能给Parent
类指定参数?我是否需要首先使用它的参数实例化Parent
类,然后在我的Child
参数中使用该对象?继承:将父类__init__参数创建为子类
class Parent(object):
def __init__(self, args):
self.args = args
class Child(Parent):
def __init__(self):
super().__init__()
object = Child()
我在哪里放父类的参数?
答
您只需将其传递给父母的__init__
调用即可。
更改Child
接受父母的参数,然后将其传递给__init__
函数。
class Child(Parent):
def __init__(self, args):
super().__init__(args)
+0
真棒,简单的东西谢谢 –
'超().__的init __()'初始化的父母,所以做'超().__的init __(东西)' – Nullman