我想问一下,如果我们使用基类定义子类,为什么需要在__init__方法中初始化父类中的参数。我和javaoop很相似,我记得在JAVA中,我们只是在子类中添加新的参数。在
如果我对Java也有错,有人能解释一下为什么要这么做吗?继承不是应该让我们的生活更容易编程吗。在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")
我的意思是,为什么电动车类中的self.battery_type = battery_type不足以进行这种继承?在