__init__() 采用 1 个位置参数,但给出了 2 个

新手上路,请多包涵

我已经阅读了有关此错误的其他帖子,我认为我已经解决了问题,但我仍然遇到问题。

我在适当的空间包含了必要的 self 参数,但我仍然收到错误:

 Traceback (most recent call last):
  File "...", line 30, in <module>
    JohnSmith = CheckingAccount(20000)
  File "...", line 18, in __init__
    BankAccount.__init__(self, initBal)
TypeError: __init__() takes 1 positional argument but 2 were given


 class BankAccount (object):
        # define class for bank account
        def __init__ (self):
            # initialize bank account w/ balance of zero
            self.balance = 0
        def deposit (self, amount):
            # deposit the given amount into account
            self.balance = self.balance + amount
        def withdraw (self, amount):
            # withdraw the given amount from account
            self.balance = self.balance - amount
        def getBalance (self):
            # return account balance
            return self.balance

class CheckingAccount (BankAccount):
    def __init__ (self, initBal):
        BankAccount.__init__(self, initBal)
        self.checkRecord = {}
    def processCheck (self, number, toWho, amount):
        self.withdraw(amount)
        self.checkRecord[number] = (toWho, amount)
    def checkInfo (self, number):
        if self.checkRecord.has_key(number):
            return self.checkRecord [ number ]
        else:
            return 'No Such Check'

# create checking account
JohnSmith = CheckingAccount(20000)
JohnSmith.processCheck(19371554951,'US Bank - Mortgage', 1200)
print (JohnSmith.checkInfo(19371554951))
JohnSmith.deposit(1000)
JohnSmith.withdraw(4000)
JohnSmith.withdraw(3500)

原文由 c_l0426 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 486
1 个回答

您可能想重新定义 BankAccount

class BankAccount(object):
    def __init__(self, init_bal=0):
        self.balance = init_bal

     # ...

原文由 chepner 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题