from abc import ABCMeta, abstractmethod class Animal(metaclass=ABCMeta): @abstractmethod def say(self): pass class Dog(Animal): def say(self): print("汪汪") class Cat(Animal): def say(self): print("喵喵") class Factory(): animal = None def get_animal(self, name): if name == "狗": animal = Dog() if name == "猫": animal = Cat() return animal if __name__ == "__main__": animal_factory = Factory() animal = animal_factory.get_animal("狗") animal.say()
标签:__,name,创建,self,模式,工厂,say,animal,class From: https://www.cnblogs.com/fuchenjie/p/17511003.html