2014-05-10 42 views
0

我看过类似的问题,但仍然无法弄清楚这一点。我绝对相信我在某个地方犯了一个非常愚蠢的错误,但我似乎无法找到它。混淆为什么我得到一个TypeError:'int'对象不可调用

对于此代码。

class BankAccount: 
    def __init__(self, initial_balance): 
     self.balance = initial_balance 

    def deposit(self, amount): 
     self.deposit = amount 
     self.balance = self.balance + self.deposit 

    def withdraw(self, amount): 
     self.withdraw = amount 
     self.balance = self.balance - self.withdraw 
     self.fee = 5 
     self.total_fees = 0 

     if self.balance < 0: 
      self.balance = self.balance - self.fee 
      self.total_fees += self.fee 

    def get_balance(self): 
     current_balance = self.balance 
     return current_balance 

    def get_fees(self): 
     return self.total_fees 

当我运行代码的一切工作正常,当我运行这个

my_account = BankAccount(10) 
my_account.withdraw(15) 
my_account.deposit(20) 
print my_account.get_balance(), my_account.get_fees() 

但是,如果我做一个额外的呼叫撤回

my_account = BankAccount(10) 
my_account.withdraw(15) 
my_account.withdraw(15) 
my_account.deposit(20) 
print my_account.get_balance(), my_account.get_fees() 

它抛出这个错误。

TypeError: 'int' object is not callable

我不明白为什么它能正常工作,直到我再打一个电话才能退出。请帮忙。

回答

4

当你这样做self.deposit = amount时,你会用金额覆盖你的deposit方法。 withdrawself.withdraw = amount相同。您需要为数据属性赋予与方法不同的名称(如调用方法withdraw,但属性withdrawalAmount或类似的东西)。

4

当你这样做withdraw方法

self.withdraw = amount 

内更换了与任何amount是它。下次您拨打withdraw时,您会收到amount对象。你的情况是int

这同样适用于deposit

self.deposit = amount 

给你的数据成员的名字是你的方法不同。

相关问题