首页 > 解决方案 > Ruby 的虚拟属性和继承超级

问题描述

我正在复习红宝石基础知识,但讲师提供的一些代码不起作用。有人可以解释一下缺少什么以及为什么需要缺少什么吗?非常感谢

class Probe
  def deploy(deploy_time, return_time)
    puts "Deploying"
  end
end

class MineralProbe < Probe
  def deploy(deploy_time)
    puts "Preparing sample chamber"
    super(deploy_time, Time.now + 2 * 60)
  end
end

Mineralprobe.new.deploy(Time.now)

我得到的错误是这样的: C:\Ruby26-x64\bin\ruby.exe C:/Users/-/RubymineProjects/test1/probe.rb Traceback (最近一次调用最后):C:/Users/-/ RubymineProjects/test1/probe.rb:14:in `': uninitialized constant Mineralprobe (NameError) 你的意思是?矿物探针

进程以退出代码 1 结束

标签: rubyinheritancevirtual-attribute

解决方案


有一个简单的类型,该类被称为

class MineralProbe < Probe
             ^

但你尝试初始化一个实例

Mineralprobe.new.deploy(Time.now)
       ^

推荐阅读