每日一题 【每日一题】玩具工厂 -Python-20211019

Jack · 2021年10月19日 · 最后由 Jack 回复于 2021年10月20日 · 52 次阅读
本帖已被设为精华帖!

用工厂模式实现一个玩具工厂,该工厂只生产猫和狗两种玩具

Jack 将本帖设为了精华贴 10月19日 22:46

参考代码:

class Toy:
    def talk(self):
        raise NotImplementedError("This method should have implemented")
class Dog(Toy):
    def talk(self):
        print("Wow")
class Cat(Toy):
    def talk(self):
        print("Meow")
class ToyFactory:
    def getToy(self, type):
        if type == "Dog":
            return Dog()
        elif type == "Cat":
            return Cat()
        return None
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册