什么是类 ClassName <::OtherClassName 在 Ruby 中做什么? [英] What does class ClassName < ::OtherClassName do in Ruby?

查看:41
本文介绍了什么是类 ClassName <::OtherClassName 在 Ruby 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我在 RSpec:

class OptionParser < ::OptionParser

这是做什么的?这和 class OptionParser <有什么区别?NameSpace::OptionParser?

What does this do? What is the difference between this and class OptionParser < NameSpace::OptionParser?

推荐答案

一个可运行的例子可能最能解释这个想法:

An runnable example might explain the idea best:

class C
  def initialize
    puts "At top level"
  end
end

module M
  class C
    def initialize
      puts "In module M"
    end
  end

  class P < C
    def initialize
      super
    end
  end

  class Q < ::C
    def initialize
      super
    end
  end
end

M::P.new
M::Q.new

运行时产生:

In module M
At top level

这篇关于什么是类 ClassName &lt;::OtherClassName 在 Ruby 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆