该my_car.drive_car()
方法旨在将ElectricCar
的成员变量更新condition
为但仍从超类"like new"
调用。drive_car
Car
my_car = ElectricCar("Flux capacitor", "DeLorean", "silver", 88)
print my_car.condition #Prints "New"
my_car.drive_car()
print my_car.condition #Prints "Used"; is supposed to print "Like New"
我错过了什么吗?有没有更优雅的方法来覆盖超类函数?
class ElectricCar
从超级继承class Car
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"