开发者

Techniques to overcome the single inheritance issue?

开发者 https://www.devze.com 2023-02-07 11:49 出处:网络
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

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

精彩评论

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