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



我正在学习一个类,他们让我第一次尝试使用ruby中的模块来实现";银行账户";程序

问题来了。。

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

让我们还创建一个不带参数的方法compound_interest。SavingsAccount和CheckingAccount都应该实现compound_interest。支票账户的利息为每月10美元,每三个月计息一次。储蓄账户的利息为每月5美元,每六个月计息一次。在compound_Interest方法中使用Interest.caCalculate_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

很抱歉这么长时间,我只想给你和我一样的信息。

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

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

这是在尝试调用一个属于Interest模块的方法。

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

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

balance = calculate_interest(balance, 3, 10)

最新更新