zoukankan      html  css  js  c++  java
  • 工厂方法

    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())
    
    工厂方法
  • 相关阅读:
    MySQL的操作
    Centos7下MySQL的安装
    一键安装Tomcat
    Hola!
    eval
    初级版笔记(修改中)
    decode前先encode(python)
    不能scanf字符串
    第一次做题的一些问题c++
    DSY3163*Eden的新背包问题
  • 原文地址:https://www.cnblogs.com/hanqian/p/6921682.html
Copyright © 2011-2022 走看看