子类继承Ruby中父类的包含模块方法

时间:2015-11-19 22:17:19

标签: ruby inheritance

我正在上课,他们第一次尝试在红宝石中使用一个模块来获得一个"银行账户"程序

这是问题..

  

创建一个名为Interest的模块。兴趣应该有一个方法calculate_interest(balance,months,interest_amount)来设置帐户的新余额。它将利息金额累计的月数倍增,将其加到余额中,然后返回新的余额。

     

让我们创建一个不带参数的方法compound_interest。 SavingsAccount和CheckingAccount都应该实现compound_interest。 CheckingAccount的利息是每月10美元,每三个月累积一次。 SavingsAccount的利息是每月5美元,每六个月累积一次。在compound_interest方法中使用Interest.calculate_interest在帐户上设置新余额。

然后我得到了这段代码......

module Interest
end

class BankAccount
  include Interest
end

class CheckingAccount < BankAccount
end

class SavingsAccount < BankAccount
end

我一直在努力弄清楚为什么我无法让它发挥作用。 这是我到目前为止所拥有的。

module Interest
  def calculate_interest(balance, months, interest_amount)
    balance += (months*interest_amount)
    balance
  end
end

class BankAccount
  attr_accessor :balance

  include Interest

  def initialize(balance)
    @balance = balance
  end
end

class CheckingAccount < BankAccount
  def initialize(balance)
    super(balance)
  end

  def compound_interest
    balance = Interest.calculate_interest(balance, 3, 10)
    balance
  end
end

class SavingsAccount < BankAccount
  def initialize(balance)
    super(balance)
  end

  def compound_interest
    balance = Interest.calculate_interest(balance, 6, 5)
    balance
  end

end

我对此非常陌生,所以我确定我的代码非常基本,请原谅。 无论如何,我一直收到这个错误......

NoMethodError
undefined method `calculate_interest' for Interest:Module
exercise.rb:24:in `compound_interest'

exercise_spec.rb:31:in `block (3 levels) in <top (required)>'

如果我的子课程继承了模块方法,我就不明白在调用模块方法时如何找到模块方法。

这里有进一步参考的规格

    describe BankAccount do
  describe "initialization" do
    it "takes an initial balance" do
      acc = BankAccount.new(283)
      expect(acc.balance).to eq(283)
    end
  end

  describe "#new" do
    it "returns the balance" do
      acc = BankAccount.new(283)
      expect(acc.balance).to eq(283)
    end
  end
end

describe CheckingAccount do
  it "has BankAccount as its parent class" do
    expect(CheckingAccount.superclass).to eq(BankAccount)
  end

  it "includes the module Interest" do
    expect(CheckingAccount.ancestors).to include(Interest)
  end

  describe "#compound_interest" do
    it "compounds the balance by the specified interest rate" do
      acc = CheckingAccount.new(283)
      new_balance = acc.compound_interest
      expect(new_balance).to eq(283 + 3 * 10)
      end
  end
end

describe SavingsAccount do
  it "has BankAccount as its parent class" do
    expect(SavingsAccount.superclass).to eq(BankAccount)
  end

  it "includes the module Interest" do
    expect(SavingsAccount.ancestors).to include(Interest)
  end

  describe "#compound_interest" do
    it "compounds the balance by the specified interest rate" do
      acc = SavingsAccount.new(283)
      new_balance = acc.compound_interest
      expect(new_balance).to eq (283 + 6 * 5)
    end
  end
end

很抱歉这么长,我只想给你相同的信息。

1 个答案:

答案 0 :(得分:0)

此条目(以及调用calculate_interest的另一点)...

balance = Interest.calculate_interest(balance, 3, 10)

这是试图调用属于Interest模块的方法。

......但是没有这样的方法。事实上,calculate_interest通过模块包含在您的类中,并成为instance方法(属于该类的每个实例的方法)。

从另一个实例方法中调用它的正确方法是没有接收器

balance = calculate_interest(balance, 3, 10)