如何使用super来引用父类的方法(变量)而不是方法(函数)

时间:2018-05-02 11:02:00

标签: python-3.x

如何使用超级方法参考汽车父类中的BMW / MERC / MAZDA库存?为什么我的股票参考代码不起作用,它只返回none的值。它适用于 init 函数以及库存检查但是当我想调用在主父类中写为变量的方法时

Python代码

class Cars(object):
    def __init__(self,name,brand,color):
        self.name=name.lower()
        self.brand=brand.lower()
        self.color=color.lower()
    BMWStock = {"black":97,"white":85,"blue":64,"red":76}
    MERCStock= {"black":100,"white":77,"blue":84,"red":66}
    MAZDAStock={"black":65,"white":76,"blue":43,"red":54}
    def stock_check(self):
        if self.brand  == "bmw":
            if self.color not in self.BMWStock:
                print ("This colour scheme for this car is unavailable at the moment, sorry.")
            else:
                for k,v in self.BMWStock.items():
                    if self.color == k:
                        print (str(v) + " models of the car is still available")
        elif self.brand  == "merc":
            if self.color not in self.MERCStock:
                print ("This colour scheme for this car is unavailable at the moment, sorry.")
            else:
                for k,v in self.MERCStock.items():
                    if self.color == k:
                        print (str(v) + " models of the car is still available")
        elif self.brand  == "mazda":
            if self.color not in self.MAZDAStock:
                print ("This colour scheme for this car is unavailable at the moment, sorry.")
            else:
                for k,v in self.MAZDAStock.items():
                    if self.color == k:
                        print (str(v) + " models of the car is still available")

class ToyCars(Cars):
    def __init__(self,name,brand,color):
        super().__init__(name,brand,color)
    BMWStock = {"black":37,"white":25,"blue":24,"red":16}
    MERCStock= {"black":40,"white":37,"blue":14,"red":26}
    MAZDAStock={"black":15,"white":26,"blue":33,"red":24}
    def stock_check(self):
        super().stock_check()
    def stock_refer(self):
        super().BMWStock



cars1=Cars("Bryan","MAZDA","white")
small_cars_1=ToyCars("bryan","merc","blue")
print (small_cars_1.stock_refer())

0 个答案:

没有答案