python在arguments = None的子类方法中调用超类方法

时间:2017-04-07 07:15:29

标签: python inheritance

我想创建一个名为ShoppingCart的类,其方法为remove_item(self,item_name,quantity,price)。

我想创建一个超类ShoppingCart的子类Shop 在Shop类中,覆盖remove_item方法,这样调用Shop的remove_item,不带参数会减少数量。

class ShoppingCart(object):
    def __init__(self):
      self.total = 0
      self.items = {}

    def add_item(self, item_name, quantity, price):
      self.total += price * quantity
      self.items[item_name] = quantity

    def remove_item(self, item_name, quantity, price):
      if quantity> self.items[item_name]:
        quantity = self.items[item_name]
        del self.items[item_name]
      else:
        new_quantity =  self.items[item_name] - quantity
        self.items[item_name] = new_quantity
      self.total-=quantity*price 

    def checkout(self, cash_paid):
      if cash_paid < self.total:
        return "Cash paid not enough"
      balance = cash_paid - self.total
      return balance

class Shop(ShoppingCart):
    def __init__(self):
      self.quantity = 100
      #ShoppingCart.__init__(self)
      super(Shop,self).__init__()

    def remove_item(self, item_name=None, quantity=None, price=None):
        self.quantity -= 1
        super(Shop, self).remove_item(item_name,quantity,price)

0 个答案:

没有答案