子类继承包含Ruby中父类的模块方法
我正在上课,他们给了我第一次尝试在Ruby中使用“Bank Account”程序的模块。子类继承包含Ruby中父类的模块方法
这里的问题..
创建一个名为兴趣模块。利息应该有一个方法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
对不起,这么长时间,我只是想给你我有同样的信息。
这个条目(和其他地步calculate_interest
称)...
balance = Interest.calculate_interest(balance, 3, 10)
这是试图调用一个方法,属于Interest
模块。
...但没有这样的方法。实际上,calculate_interest
通过模块包含在您的课程中,并成为instance
方法(属于该类的每个实例的方法)。
正确的方法从另一个实例方法中调用它是没有一个接收器
balance = calculate_interest(balance, 3, 10)
我想我很困惑,因为这些说明告诉我在这些方法中使用Interest.calculate_interest。我尝试过这个解决方案,但仍然失败并出现新的错误。 '''NoMethodError' '未定义方法'+'为零:NilClass''' –
是的,恭喜你,你已经过了第一个错误!看起来'balance'包含nil,所以你不能给它添加一个数字。尝试引用attr_accessor方法而不是创建局部变量。 'self.balance = calculate_interest(self.balance,3,10)' – SteveTurczyn
我想通了。我需要在模块方法中使用@balance实例变量而不是“balance”。随着你的建议,它的工作。谢谢! –
提示:什么是对象的规格调用'compound_interest'上? *你*调用'compound_interest'的对象是什么?什么是您打电话给'compound_interest'的对象的类?那个类或它的任何祖先是否有一个'compound_interest'方法? –
不幸的是,“冗长”与“特定”并不相同,实际上恰恰相反。请参阅:[如何创建最小,完整和可验证示例](http://*.com/help/mcve)。在你的问题中有许多无关的绒毛与手头的问题无关,并且很难看出* actual *问题在哪里。(我只是偶然碰巧碰到它。) –
你失去了我。我也不太了解规格。 –