【发布时间】:2015-05-24 02:26:36
【问题描述】:
我在理解 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
?
【问题讨论】:
标签: python inheritance overriding