class BankAccount: def __init__(self, acct_number, acct_name): self.acct_number = acct_number self.acct_name = acct_name self.balance = 0.0 def displayBalance(self): print "The account balance is:", self.balance def deposit(self, amount): self.balance = self.balance + amount print "You deposited", amount print "The new balance is:", self.balance def withdraw(self, amount): if self.balance >= amount: self.balance = self.balance - amount print "You withdrew", amount print "The new balance is:", self.balance else: print "You tried to withdraw", amount print "The account balance is:", self.balance print "Withdrawal denied. Not enough funds." class InterestAccount(BankAccount): def addInterest(self, rate): interest = self.balance * rate print "adding interest to the account,", rate * 100,"percent" self.deposit (interest) myAccount = BankAccount(234, "Warren Sande") print "Account name:",myAccount.acct_name print "Account number:", myAccount.acct_number myAccount.displayBalance() myAccount.deposit(34.52) myAccount.addInterest(0.11)
运行时提示:
Traceback (most recent call last):
File "D:\Workspaces\MyEclipse Professional 2014\NO1\helloworld\__init__.py", line 46, in <module>
myAccount.addInterest(0.11)
AttributeError: BankAccount instance has no attribute 'addInterest'
不知为何出错,请指教。
小猫过河
有心寻性
小猫过河
相关分类