fork 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. def info(self):
  26. return "Cal => v1 : %d, v2 : %d" % (self.v1, self.v2)
  27. class CalMultiply(Cal):
  28. def multiply(self):
  29. result = self.v1*self.v2
  30. Cal._history.append("multiply : %d*%d=%d" % (self.v1, self.v2, result))
  31. return result
  32. def info(self):
  33. return "CalMultiply => %s" % super().info()
  34. class CalDivide(CalMultiply):
  35. def divide(self):
  36. result = self.v1/self.v2
  37. Cal._history.append("divide : %d/%d=%d" % (self.v1, self.v2, result))
  38. return result
  39. def info(self):
  40. return "CalDivide => %s" % super().info()
  41.  
  42. c0 = Cal(30, 60)
  43. print(c0.info())
  44. c1 = CalMultiply(10,10)
  45. print(c1.info())
  46. c2 = CalDivide(20,10)
  47. print(c2.info())
  48.  
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
Cal => v1 : 30, v2 : 60
CalMultiply => Cal => v1 : 10, v2 : 10
CalDivide => CalMultiply => Cal => v1 : 20, v2 : 10