加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby中的委托是什么?

发布时间:2020-12-17 01:31:43 所属栏目:百科 来源:网络整理
导读:我在教科书中遇到过这个问题,但我甚至不知道代表团是什么.我知道包含是什么,但不知道代表团是什么. In the context of Ruby,compare delegation to module inclusion in terms of the notion of class interfaces. With module inclusion,methods defined in
我在教科书中遇到过这个问题,但我甚至不知道代表团是什么.我知道包含是什么,但不知道代表团是什么.

In the context of Ruby,compare delegation to module inclusion in
terms of the notion of class interfaces.

With module inclusion,methods defined in modules become part of the
interface of classes(and all their subclasses). This is not the case
with delegations.

你能用外行的话解释一下吗?

解决方法

简单地说,委托就是当一个对象使用另一个对象进行方法调用时.

如果您有这样的事情:

class A
  def foo
    puts "foo"
  end
end

class B
  def initialize
    @a = A.new
  end

  def bar
    puts "bar"
  end

  def foo
    @a.foo
  end
end

当调用foo方法时,B类的一个实例将使用A类的foo方法.换句话说,B的实例将foo方法委托给A类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读