fork(1) download
  1. class Cal(object):
  2. _history = []
  3. def __init__(self, v1, v2):
  4. if isinstance(v1, int):
  5. self.v1 = v1
  6. if isinstance(v2, int):
  7. self.v2 = v2
  8. def add(self):
  9. result = self.v1+self.v2
  10. Cal._history.append("add : %d+%d=%d" % (self.v1, self.v2, result))
  11. return result
  12. def subtract(self):
  13. result = self.v1-self.v2
  14. Cal._history.append("subtract : %d-%d=%d" % (self.v1, self.v2, result))
  15. return result
  16. def setV1(self, v):
  17. if isinstance(v, int):
  18. self.v1 = v
  19. def getV1(self):
  20. return self.v1
  21. @classmethod
  22. def history(cls):
  23. for item in Cal._history:
  24. print(item)
  25. class CalMultiply(Cal):
  26. def multiply(self):
  27. result = self.v1*self.v2
  28. Cal._history.append("multiply : %d*%d=%d" % (self.v1, self.v2, result))
  29. return result
  30. class CalDivide(CalMultiply):
  31. def divide(self):
  32. result = self.v1/self.v2
  33. Cal._history.append("divide : %d/%d=%d" % (self.v1, self.v2, result))
  34. return result
  35. c1 = CalMultiply(10,10)
  36. print(c1.add())
  37. print(c1.multiply())
  38. c2 = CalDivide(20,10)
  39. print(c2, c2.add())
  40. print(c2, c2.multiply())
  41. print(c2, c2.divide())
  42. Cal.history()
  43.  
Success #stdin #stdout 0.02s 8688KB
stdin
Standard input is empty
stdout
20
100
<__main__.CalDivide object at 0xb755914c> 30
<__main__.CalDivide object at 0xb755914c> 200
<__main__.CalDivide object at 0xb755914c> 2.0
add : 10+10=20
multiply : 10*10=100
add : 20+10=30
multiply : 20*10=200
divide : 20/10=2