我有两类,一类是Vehicle,一类是BMW,我知道我可以在Vehicle的属性上创建颜色。但是我如何在BMW类中设置属性
但我仍然能够以bmw.info,color
的身份访问颜色?我可以做类似的事情吗?
class Vehicle:
def __init__(self, name):
self.name = name
self._color = None
@property
def color(self):
return self._color
@color.setter
def color(self, color):
self._color = color
class BMW:
def __init__(self, name):
self.info = Vehicle(name)
#can I do something like
class Vehicle:
def __init__(self, name):
self.name = name
class BMW:
def __init__(self, name):
self.info = Vehicle(name)
setattr(self.info, "color", None)
@info.property
def color(self):
return self._color
@info.color.setter
def color(self, color):
self._color = color
bmw = BMW('sister')
print(bmw.info.color)
bmw.info.color = 'red'
print(bmw.info.color)