【发布时间】:2018-01-23 17:48:31
【问题描述】:
我需要让子类从超类 Vehicle 中获取信息并进入它们相应的类并携带信息。但是我不能让他们延续到新的班级。我需要做什么才能允许继承特定特征?
class Vehicle:
def __init__(self, make , model):
self.year = 2000
self.make = make
self.model = model
# a vehicle is instantiated with a make and model
@property
def year(self):
return self._year
print "year"
#mutator
@year.setter
def year(self, value):
if (value >= 2000):
self._year = value
if (value <= 2018):
self._year = value
if (value > 2018):
self.value = 2018
else:
self._year = 2000
pass
class Truck(Vehicle):
pass
class Car(Vehicle):
pass
class DodgeRam(Truck):
pass
class HondaCivic(Car):
pass
ram = DodgeRam(2016)
print ram
civic1 = HondaCivic(2007)
print civic1
civic2 = HondaCivic(1999)
print civic2
【问题讨论】:
-
请发布您到目前为止所尝试的内容。
-
什么,确切地,不起作用?
-
注意,你不小心做了
self.value = 2018而不是self._year = 2018 -
我不能让汽车/卡车继承那一年,然后去道奇拉姆/本田思域
-
我真正需要知道的是如何从另一个类继承一些东西
标签: python python-2.7