class Operation(): def __init__(self, NumberA=0, NumberB=0): self.NumberA = NumberA self.NumberB = NumberB def get_result(self): pass class AddOp(Operation): def get_result(self): return self.NumberB + self.NumberA class MinusOp(Operation): def get_result(self): return self.NumberA - self.NumberB class MultiOp(Operation): def get_result(self): return self.NumberA * self.NumberB class DivideOp(Operation): def get_result(self): try: return self.NumberA / self.NumberB except ZeroDivisionError: raise class OperationFatory(): def create_OP(self): pass class AddFactory(OperationFatory): def create_OP(self): return AddOp() class MinusFactory(OperationFatory): def create_OP(self): return MinusOp() if __name__ == '__main__': factory = MinusFactory() f = factory.create_OP() f.NumberA = 3 f.NumberB = 5 print(f.get_result()) 工厂方法