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

Ruby-嵌套类和子类是一回事吗?

发布时间:2020-12-17 03:33:13 所属栏目:百科 来源:网络整理
导读:在下面的示例中,嵌套和Child之间有什么区别?是同一件事的不同语法吗? class Parent class Nested ... endendclass Child Parent ...end 解决方法 不,他们是不同的. 嵌套:计算机外部的“处理器”类只能作为Computer :: Processor访问.嵌套为内部类(命名空
在下面的示例中,嵌套和Child之间有什么区别?是同一件事的不同语法吗?

class Parent
  class Nested
    ...
  end
end

class Child < Parent
  ...
end

解决方法

不,他们是不同的.

嵌套:计算机外部的“处理器”类只能作为Computer :: Processor访问.嵌套为内部类(命名空间)提供上下文.对于ruby解释器Computer and Computer :: Processor只是两个独立的类.

class Computer
  class Processor # To create an object for this class,this is the syntax Computer::Processor.new. The Outer class provides context

Child:下面是类继承,Parent类的实例/类方法可用于Child.可以像这样Child.new/Parent.new实例化Child / Parent

class Child < Parent

请注意,处理器只能由Computer :: Processor访问,只是调用Processor会抛出错误.类似地,调用Child是好的,但调用Parent :: Child会抛出一个警告(虽然它实际上会运行正常).

(编辑:李大同)

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

    推荐文章
      热点阅读