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

ruby – 在模块中获得类的可见性的最佳方法

发布时间:2020-12-17 02:59:10 所属栏目:百科 来源:网络整理
导读:有关最佳方法的建议,不需要使用’::’来使用模块中的类(如下所示)? 使用import / mixin?这不是一个多继承案例吗?只想简化代码.在这种情况下使用导入是否有副作用? module Shapes class Circle class RightTriangle endclass ShapeUser def go shape1 = S
有关最佳方法的建议,不需要使用’::’来使用模块中的类(如下所示)?

使用import / mixin?这不是一个多继承案例吗?只想简化代码.在这种情况下使用导入是否有副作用?

module Shapes  
    class Circle  
    class RightTriangle  
end

class ShapeUser  
    def go  
        shape1 = Shapes::Circle.new   
        ** prefer to use just shape1 = Circle.new
        ....
end

解决方法

包括模块:

class ShapeUser
  include Shapes

  def go
    shape1 = Circle.new
    # ...
  end
end

这里发生的一切就是你正在为ShapeUser类提供常量Circle,RightTriangle等.这是一个完全合理的使用包括:)

(编辑:李大同)

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

    推荐文章
      热点阅读