成员变量condition
应该从 更改"new"
为"like new"
一次my_car.drive_car()
被调用。但是,此调用仍然从超类执行drive_car
函数,class Car
而不是class ElectricCar
- 我错过了什么吗?
- 有没有更优雅的方法来覆盖超类函数?
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model, self.color, self.mpg = model, color, mpg
def drive_car(self):
self.condition = "used"
class ElectricCar(Car):
def __init__(self, battery_type, model, color, mpg):
self.battery_type = battery_type
super(ElectricCar, self).__init__(model, color, mpg)
def drive_car(self):
self.condition = "like new"
my_car = ElectricCar("Flux capacitor", "DeLorean", "silver", 88)
print my_car.condition #Prints "New"
my_car.drive_car()
print my_car.condition #Prints "Used" instead of "Like New"