继承:将父类__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() 

我在哪里放父类的参数?

+0

'超().__的init __()'初始化的父母,所以做'超().__的init __(东西)' – Nullman

您只需将其传递给父母的__init__调用即可。

更改Child接受父母的参数,然后将其传递给__init__函数。

class Child(Parent): 
    def __init__(self, args): 
     super().__init__(args) 
+0

真棒,简单的东西谢谢 –