GitHub 博客园 Nanakon

混合使用静态方法、类方法和抽象方法

import abc

class BasePizza(object):
    __metaclass__ = abc.ABCMeta
    @abc.abstractmethod
    def get_ingredients(self):
        """Returns the ingredient list."""

class Calzone(BasePizza):
    def get_ingredients(self, with_egg=False):
        egg = Egg() if with_egg else None
        return self.ingredients + [egg]
    
class DietPizza(BasePizza):
    @staticmethod
    def get_ingredients():
        return None

 

import abc

class BasePizza(object):
    __metaclass__ = abc.ABCMeta
    default_ingredients = ['cheese']

    @classmethod
    @abc.abstractmethod
    def get_ingredients(cls):
        """Returns the default ingredient list."""
        return cls.default_ingredients

class DietPizza(BasePizza):
    def get_ingredients(self):
        return [Egg()] + super(DietPizza, self).get_ingredients()

 

posted on 2017-03-29 13:09  jzm17173  阅读(166)  评论(0编辑  收藏  举报

导航

轻音