Child class initialization of arguments
我想问一下,如果我们使用基类定义子类,为什么我们需要在
如果我对Java也是错误的,有人能解释一下为什么要这么做吗?继承不是让我们的生活更容易编程的东西吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Car(object): condition ="new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg def display_car(self): print"This is a %s %s with %s MPG." % (self.color, self.model, self.mpg) def drive_car(self): self.condition ="used" class ElectricCar(Car): def __init__(self, model, color, mpg, battery_type): self.model = model self.color = color self.mpg = mpg self.battery_type = battery_type my_car = ElectricCar("Auris","golden", 89,"molten salt") |
我的意思是,为什么ElectricCar类中的EDOCX1[1]不足以进行这种继承?
你可以在overridden
1 2 3 4 | class ElectricCar(Car): def __init__(self, model, color, mpg, battery_type): super(ElectricCar, self).__init__(model, color, mpg) self.battery_type = battery_type |
如果你使用的是Python 3类,你可以在omit和自参考文献:
1 2 3 4 | class ElectricCar(Car): def __init__(self, model, color, mpg, battery_type): super().__init__(model, color, mpg) self.battery_type = battery_type |
无论是单的,给你一
你可以写:
1 2 3 4 | class ElectricCar(Car): def __init__(self, model, color, mpg, battery_type): super(ElectricCar, self).__init__(model, color, mpg) self.battery_type = battery_type |