如果我有一个 python 类:
class BaseClass(object):
#code and the init function of the base class
然后我定义一个子类,比如:
class ChildClass(BaseClass):
#here I want to call the init function of the base class
如果基类的 init 函数接受一些参数,我将它们作为子类的 init 函数的参数,那么如何将这些参数传递给基类?
我写的代码是:
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
class ElectricCar(Car):
def __init__(self, battery_type, model, color, mpg):
self.battery_type=battery_type
super(ElectricCar, self).__init__(model, color, mpg)
我哪里做错了?