class car:
def __init__(self,model,year):
self.model = model
self.year = year
class BMW(car):
def __init__(self,type,model,year):
car.__init__(self,model,year)
self.type = type
class Audi(car):
def __init__(self,type1,model,year):
car.__init__(self, model, year)
self.type1 = type1
d500 = BMW('manual','500d',2020)
print(BMW.type)
print(BMW.model)
print(BMW.year)发布于 2020-11-03 23:14:40
您在这里并没有真正提出问题,但是您可能想知道为什么抛出错误AttributeError: type object 'BMW' has no attribute 'type'。
您正在使用:d500 = BMW('manual','500d',2020)实例化一个BMW实例。然而,在随后的几行中,您引用的是类本身,而不是已实例化的对象。
由于在car/BMW的构造函数中设置了model、year和type,因此没有定义BMW.type。
您需要调用:
print(d500.type)
print(d500.model)
print(d500.year)而是为了引用新创建的对象。
发布于 2020-11-03 23:14:36
您正试图从BMW打印type,但您只是将该对象设置为变量d500。请改用d500来访问属性。
d500 = BMW('manual','500d',2020)
print(d500.type)
print(d500.model)
print(d500.year)https://stackoverflow.com/questions/64665489
复制相似问题