开发者

Techniques to overcome the single inheritance issue?

what techniques are there in ruby to overcome the single inheritance rule?

开发者_如何学运维

is it just using modules?


Yes, modules are the answer in general. If you have a more specific needs (such as having a class that merges functionality from two different classes) other options exist, such as delegation.

Note that if you need a module to provide 'class' methods to those that inherit from it, you will probably want to use this common pattern:

module Foo
  def aaa
    "hi"
  end
  module ClassMethods
    def bbb
      "mom"
    end
  end
  def self.included( other )
    other.extend ClassMethods
  end
end

class Bar
  include Foo
end

puts Bar.new.aaa, Bar.bbb
#=> hi
#=> mom
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜