[英]AttributeError: type object 'BMW' has no attribute 'type'
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)
您还没有真正在这里提出问题,但大概您想知道为什么会抛出错误AttributeError: type object 'BMW' has no attribute 'type'
。
您正在实例化BMW
一个实例: d500 = BMW('manual','500d',2020)
。 但是,在随后的几行中,您指的是类本身,而不是您实例化的对象。
由于在car
/ BMW
的构造函数中设置了model
、 year
和type
,因此BMW.type
。
您需要致电:
print(d500.type)
print(d500.model)
print(d500.year)
而是为了引用您新创建的对象。
您正在尝试从BMW
打印type
,但您只是将该对象设置为变量d500
。 请改用d500
来访问属性。
d500 = BMW('manual','500d',2020)
print(d500.type)
print(d500.model)
print(d500.year)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.