[英]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)
You haven't really asked a question here, but presumably you're wondering why the error AttributeError: type object 'BMW' has no attribute 'type'
is being thrown.您还没有真正在这里提出问题,但大概您想知道为什么会抛出错误
AttributeError: type object 'BMW' has no attribute 'type'
。
You're instantiating an instance of BMW
with: d500 = BMW('manual','500d',2020)
.您正在实例化
BMW
一个实例: d500 = BMW('manual','500d',2020)
。 However, in subsequent lines, you're referring to the class itself rather than the object that you've instantiated.但是,在随后的几行中,您指的是类本身,而不是您实例化的对象。
Since model
, year
and type
are set in the constructor for car
/ BMW
, BMW.type
is not defined.由于在
car
/ BMW
的构造函数中设置了model
、 year
和type
,因此BMW.type
。
You need to call:您需要致电:
print(d500.type)
print(d500.model)
print(d500.year)
instead in order to refer to your newly created object.而是为了引用您新创建的对象。
You're trying to print the type
from BMW
, but you just set that object to the variable d500
.您正在尝试从
BMW
打印type
,但您只是将该对象设置为变量d500
。 Use d500
instead to access the attributes.请改用
d500
来访问属性。
d500 = BMW('manual','500d',2020)
print(d500.type)
print(d500.model)
print(d500.year)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.