<分区>
我在理解 Python 中的继承时遇到了困难,但由于我在 Java 方面的经验比较丰富,所以我知道它是如何工作的……明确地说,我在这里搜索了问题以及在线文档,所以我知道这将立即被标记为重复问题 :P
我在 Codecademy 上的代码是这样通过的:
class Car(object):
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "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
但据我所知,我几乎是在定义一个新类……其中的继承在哪里?我可以做类似的事情吗:
class ElectricCar(Car):
def __init__(self, battery_type):
self.model = model
self.color = color
self.mpg = mpg
self.battery_type = battery_type
也许有关键字
super
?