【发布时间】:2013-04-03 20:38:17
【问题描述】:
我正在制作一个程序,它使用 Account 类来打印 accountA 的每月利息金额等。我在解决 getMonthlyInterestRate() 和 getMonthlyInterest 定义时遇到问题。这是迄今为止的程序:
帐户.py
class Account:
def __init__(self,id=0,balance=100.0,annualInterestRate=0.0):
self.__id=id
self.__balance=balance
self.__annualInterestRate=annualInterestRate
def getId(self):
return self.__id
def getBalance(self):
return self.__balance
def getAnnualInterest(self):
return self.__annualInterestRate
def setId(self,newid):
self.__id=newid
def setBalance(self,newbalance):
self.__balance=newbalance
def setAnnualInterestRate(self,newannualInterestRate):
self.__annualInterestRate=newannualInterestRate
def getMonthlyInterestRate(self,getAnnualInterest):
return(getAnnualInterest(self)/12)
def getMonthlyInterest(self,getBalance,getMonthly):
return(getBalance(self)*getMonthlyInterestRate(self))
def withdraw(self,amount):
if(amount<=self.__balance):
self.__balance=self.__balance-amount
def deposit(self,amount):
self.__balance=self.__balance+amount
def __str__(self):
return "Account ID : "+str(self.__id)+" Account Balance : "+str(self.__balance)+" Annual Interest Rate : "+str(self.__annualInterestRate)
下一个
文件 test.py
from Account import Account
def main():
accountA=Account(0,100,0)
accountA.setId(1234)
accountA.setBalance(20500)
accountA.setAnnualInterestRate(0.375)
print(accountA.__str__())
accountA.withdraw(500)
accountA.deposit(1500)
print(accountA.__str__())
print(accountA.getMonthlyInterest(accountA.getBalance(),accountA.getAnnualInterest()))
main()
我不知道如何使 getMonthlyInterestRate() 和 getMonthlyInterest() 定义能够输出正确的输出,即:
Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375
Monthly Interest Amount : 671.875
我的总是报错:
Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 13, in <module>
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 12, in main
File "C:\Users\Meagan\Documents\University\2nd Year\Cmput 174\Account.py", line 21, in getMonthlyInterest
return(getBalance(self)*getMonthlyInterestRate(self))
builtins.TypeError: 'int' object is not callable
这是我应该做的:
一个名为 getMonthlyInterestRate() 的方法,它返回月利率。
一个名为 getMonthlyInterest() 的方法,它返回每月的利息金额。每月利息金额可以通过使用余额*每月利率来计算。月利率可以通过年利率除以 12 来计算。
除了这两个定义和最后一个打印语句之外,程序中的其他所有内容都是正确的。任何帮助,将不胜感激。谢谢。
【问题讨论】:
-
打印时不需要直接调用
.__str__();print会为你调用,所以print(accountA)就足够了。 -
也不需要用
self调用方法。它是自动完成的...... -
它不是函数...类函数是方法
-
@foriinrangeawesome:其实
getBalance是 一个函数,只是它不在正确的locals命名空间中。 (这在 2.x 中并不完全正确,它具有未绑定的方法,但差异并不重要;在 3.x 中确实如此。)因此,如果您执行Account.getBalance(self)或getBalance = self.__class__.getBalance; getBalance(self)或其他任何操作解决这个问题,它将与self.getBalance()完全相同。当然,仅仅因为您可以将其作为函数而不是绑定方法调用并不意味着您应该... -
@Ben:如果你使用方法调用语法,它只会自动完成(所以你会得到一个绑定的方法)。只是
getBalance()是行不通的;你需要self.getBalance()(或getBalance = self.getBalance; getBalance()或类似的东西)。
标签: python class function balance