【问题标题】:Parent and child class construction父子类构建
【发布时间】:2016-11-13 08:38:39
【问题描述】:

我只是在检查我是否正确地为这个检查类编写了我的代码,并且确实检查类正确地访问了 Account。我只需要正确初始化它。

class Checking < Account

    def 
       super
    end

    def balance()
        @balance = principal * (1 + interest_rate / 365) ** 365
    end

end

【问题讨论】:

  • 你遇到了什么错误
  • class Checking &lt; Account end 足够了......你的问题很可能是因为rails没有找到你的定义 - 你把它放在哪里,错误在哪里被抛出?我不知道您所说的“因为程序是预先编写的”是什么意思……而不是在运行时动态生成?
  • 如果对解释有帮助,我可以将整个代码粘贴到链接中。
  • 我认为没有必要这样做,除非它与问题相关。
  • 由于您使用的是 Rails,所以这个问题需要更具体地了解 Rails 位。您将这两个类放在 Rails 文件层次结构中的什么位置?它们是在同一个文件中,还是在不同的文件中,在哪里?还请务必包含确切的错误消息。

标签: ruby-on-rails ruby


【解决方案1】:

您错过了initialize。变化:

class Checking < Account
  def 
     super
  end

  def balance()
    @balance = principal * (1 + interest_rate / 365) ** 365
  end
end

class Checking < Account
  def initialize
     super
  end

  def balance
    @balance = principal * (1 + interest_rate / 365) ** 365
  end
end

您的下一个问题将是 Checking#new (initialize) 不接受参数,但您调用 super 并且 Account#new 需要一个参数。

【讨论】:

    【解决方案2】:

    我已经弄清楚我做错了什么,我最终纠正了它。谢谢你们的意见

    class Checking < Account
         @@interest_rate = value
      def initialize(method)
         super(parameters)
          @principal = interest_rate
      end
    
      def balance
        @principal = principal * (1 + interest_rate / 365) ** 365
      end
    end
    

    这就是我正在寻找的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多