繁体   English   中英

属性错误:类型对象“宝马”没有属性“类型”

[英]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的构造函数中设置了modelyeartype ,因此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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM