Python Factory方法和Abstract类

时间:2018-04-21 13:30:10

标签: python oop inheritance abstract-class factory

我刚刚在网站上看到了这个代码示例:

class Shape(object):
    # Create based on class name:
    def factory(type):
        #return eval(type + "()")
        if type == "Circle": return Circle()
        if type == "Square": return Square()
        assert 0, "Bad shape creation: " + type
    factory = staticmethod(factory)

class Circle(Shape):
    def draw(self): print("Circle.draw")
    def erase(self): print("Circle.erase")

class Square(Shape):
    def draw(self): print("Square.draw")
    def erase(self): print("Square.erase")

我很困惑。不会Square和Circle继承方法工厂,因为它们是从Shape继承的吗? 如果是这样,我不理解它背后的逻辑,因为工厂应该只是形状。

0 个答案:

没有答案