Advertisement
stream13

Python type transmission example

Dec 14th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. class MathOperator(object):
  5.     def __init__(self, name):
  6.         self.result = 0
  7.         print(name)
  8.  
  9.     def work(self, a, b): pass
  10.  
  11.     def get(self): return self.result
  12.  
  13.  
  14. class OperationSum(MathOperator):
  15.  
  16.     def __init__(self): super(OperationSum, self).__init__('OperationSum')
  17.  
  18.     def work(self, a, b): self.result = a + b
  19.  
  20.  
  21. class OperationMult(MathOperator):
  22.  
  23.     def __init__(self): super(OperationMult, self).__init__('OperationMult')
  24.  
  25.     def work(self, a, b): self.result = a * b
  26.  
  27.  
  28. class Executor(object):
  29.  
  30.     def __init__(self, _type):
  31.         self.operator = _type()
  32.  
  33.     def pr(self, a, b):
  34.         self.operator.work(a, b)
  35.         print(str(self.operator.get()))
  36.  
  37.  
  38. e1 = Executor(OperationSum)
  39. e1.pr(5, 6)
  40.  
  41. e2 = Executor(OperationMult)
  42. e2.pr(5, 6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement