我需要让子类从超类车辆中获取信息,并进入相应的类并携带信息。但是我不能让他们继续上新的课程。我需要做些什么来允许特定特征的遗传?在
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
根据你的评论,“我真正需要知道的是如何从另一个类继承一些东西”
这对我很有效。年份的继承是自动的,2000年是由Python2号和Python3号同时打印的。在
你的问题似乎是,你在个人汽车制造水平上投入了年投入,但却试图在超级水平上储存这些价值。应将值存储在共享的最低级别:
^{pr2}$如果您想强制子类具有某些值(make/model),而不是将它们留空,可以看到Enforcing Class Variables in a Subclass
相关问题 更多 >
编程相关推荐