博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单工厂模式
阅读量:4635 次
发布时间:2019-06-09

本文共 1647 字,大约阅读时间需要 5 分钟。

1 class Operation(): 2  3     def __init__(self, NumberA=0, NumberB=0): 4         self.NumberA = NumberA 5         self.NumberB = NumberB 6  7     def GetResult(self): 8         pass 9 10 11 class AddOp(Operation):12 13     def GetResult(self):14         return self.NumberB + self.NumberA15 16 17 class MinusOp(Operation):18 19     def GetResult(self):20         return self.NumberA - self.NumberB21 22 23 class MultiOp(Operation):24 25     def GetResult(self):26         return self.NumberA * self.NumberB27 28 29 class DivideOp(Operation):30 31     def GetResult(self):32         try:33             return 1.0 * self.NumberA / self.NumberB34         except ZeroDivisionError:35             raise36 37 38 class OperationFatory():39 40     def ChooseOperation(self, op):41         if op == '+':42             return AddOp()43         if op == '-':44             return MinusOp()45         if op == '*':46             return MultiOp()47         if op == '/':48             return DivideOp()49 50 51 if __name__ == '__main__':52     ch = ''53     while not ch == 'q':54         NumberA = float(input('Please input number1:  '))55         op = input('Please input the operation:  ')56         NumberB = float(input('Please input number2:  '))57         OPFactory = OperationFatory()58         OPType = OPFactory.ChooseOperation(op)59         OPType.NumberA = NumberA60         OPType.NumberB = NumberB61         print('The result is:', OPType.GetResult())62         print('\n#--  input q to exit any key to continue')63         try:64             ch = input()65         except:66             ch = ''

 

转载于:https://www.cnblogs.com/gundan/p/8288824.html

你可能感兴趣的文章
⑥python模块初识、pyc和PyCodeObject
查看>>
object-c中管理文件和目录:NSFileManager使用方法
查看>>
Kibana:分析及可视化日志文件
查看>>
nodejs pm2使用
查看>>
cocos2d-x 3.10 PageView BUG
查看>>
装饰器的基本使用:用户登录
查看>>
CSS选择器总结
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
ABAP 程序间的调用
查看>>
移动端单屏解决方案
查看>>
web渗透测试基本步骤
查看>>
把mysql 中的字符gb2312 改为gbk的方法
查看>>
使用Struts2标签遍历集合
查看>>