返回Ruby子类中重写方法的数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了返回Ruby子类中重写方法的数组相关的知识,希望对你有一定的参考价值。

  1. # thanks to the answer here:
  2. # http://stackoverflow.com/questions/3488203/how-do-i-see-where-in-the-class-hierarchy-a-method-was-defined-and-overridden-in
  3. class Object
  4. def self.overridden_methods(parent_class = Object, within_tree = true)
  5. if within_tree
  6. defined_methods = ancestors[0..ancestors.index(parent_class) - 1].map { |object| object.instance_methods(false) }.flatten.uniq
  7. parent_methods = superclass.instance_methods
  8. else
  9. defined_methods = instance_methods(false)
  10. parent_methods = parent_class.instance_methods
  11. end
  12. defined_methods & parent_methods
  13. end
  14.  
  15. def overridden_methods(parent_class = Object, within_tree = true)
  16. self.class.overridden_methods(parent_class, within_tree)
  17. end
  18.  
  19. def self.overrides?(method, parent_class = Object, within_tree = true)
  20. overridden_methods(parent_class, within_tree).include?(method.to_s)
  21. end
  22.  
  23. def overrides?(method, parent_class = Object, within_tree = true)
  24. self.class.overrides?(method, parent_class, within_tree)
  25. end
  26. end
  27.  
  28. class ParentModel < Object
  29. def save; end
  30. end
  31.  
  32. class ChildModel < ParentModel
  33. def save; end
  34. def create; end
  35. def to_s; end
  36. end
  37.  
  38. class AnotherChildModel < ParentModel
  39. def save; end
  40. end
  41.  
  42. class GrandChildModel < ChildModel
  43. def create; end
  44. end
  45.  
  46. # what have you overridden from the base `Object` class
  47. p ParentModel.overridden_methods(Object)
  48. #=> []
  49. p ParentModel.overridden_methods(Object, false)
  50. #=> []
  51. p ChildModel.overridden_methods(Object)
  52. #=> ["to_s", "save"]
  53. p ChildModel.overridden_methods(Object, false)
  54. #=> ["to_s"]
  55. p ChildModel.overridden_methods(ParentModel)
  56. #=> ["to_s", "save"]
  57. p ChildModel.overridden_methods(ParentModel, false)
  58. #=> ["to_s", "save"]
  59. p AnotherChildModel.overridden_methods(Object)
  60. #=> ["save"]
  61. p AnotherChildModel.overridden_methods(Object, false)
  62. #=> []
  63. p AnotherChildModel.overridden_methods(ParentModel)
  64. #=> ["save"]
  65. p AnotherChildModel.overridden_methods(ParentModel, false)
  66. #=> ["save"]
  67. p GrandChildModel.overridden_methods(Object)
  68. #=> ["create", "to_s", "save"]
  69. p GrandChildModel.overridden_methods(Object, false)
  70. #=> []
  71. p GrandChildModel.overridden_methods(ParentModel)
  72. #=> ["create", "to_s", "save"]
  73. p GrandChildModel.overridden_methods(ParentModel, false)
  74. #=> []
  75. p GrandChildModel.overridden_methods(ChildModel)
  76. #=> ["create"]
  77. p GrandChildModel.overridden_methods(ChildModel, false)
  78. #=> ["create"]
  79.  
  80. # note, this probably won't work with modules overriding methods, it just needs to be tweaked a little more

以上是关于返回Ruby子类中重写方法的数组的主要内容,如果未能解决你的问题,请参考以下文章

Java—重写与重载的区别

java重写和重载

java 子类方法重载和重写区别

17.面向对象-----方法的重写

8.2java 方法重写和属性重写

24. java面向对象 - 方法重写