fork download
  1. class Cal
  2. attr_reader :v1, :v2
  3. attr_writer :v1
  4. @@_history = []
  5. def initialize(v1,v2)
  6. @v1 = v1
  7. @v2 = v2
  8. end
  9. def add()
  10. result = @v1+@v2
  11. @@_history.push("add : #{@v1}+#{@v2}=#{result}")
  12. return result
  13. end
  14. def subtract()
  15. result = @v1-@v2
  16. @@_history.push("subtract : #{@v1}-#{@v2}=#{result}")
  17. return result
  18. end
  19. def setV1(v)
  20. if v.is_a?(Integer)
  21. @v1 = v
  22. end
  23. end
  24. def getV1()
  25. return @v1
  26. end
  27. def Cal.history()
  28. for item in @@_history
  29. p item
  30. end
  31. end
  32. def info()
  33. return "Cal => v1 : #{@v1}, v2 : #{@v2}"
  34. end
  35. end
  36. class CalMultiply < Cal
  37. def multiply()
  38. result = @v1*@v2
  39. @@_history.push("multipy : #{@v1}*#{@v2}=#{result}")
  40. return result
  41. end
  42. def info()
  43. return "CalMultiply => #{super()}"
  44. end
  45. end
  46. class CalDivide < CalMultiply
  47. def divide()
  48. result = @v1/@v2
  49. @@_history.push("divide : #{@v1}/#{@v2}=#{result}")
  50. return result
  51. end
  52. def info()
  53. return "CalDivide => #{super()}"
  54. end
  55. end
  56. c0 = Cal.new(30, 60)
  57. p c0.info()
  58. c1 = CalMultiply.new(10, 10)
  59. p c1.info()
  60. c2 = CalDivide.new(20, 10)
  61. p c2.info()
  62.  
Success #stdin #stdout 0.05s 9736KB
stdin
Standard input is empty
stdout
"Cal => v1 : 30, v2 : 60"
"CalMultiply => Cal => v1 : 10, v2 : 10"
"CalDivide => CalMultiply => Cal => v1 : 20, v2 : 10"