我在python中有以下代码行:
class BankAccount:
def __init__ (self,balance = 0):
self.balance = balance
def deposit(self, deposit_amount= 30):
self.deposit_amount=deposit_amount
balance += deposit_amount
return balance
def withdraw (self,withdraw_amount= 10):
if withdraw_amount > balance:
raise RuntimeError('Invalid Transaction')
balance -= withdraw_amount
return balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
它给了我这个错误:
AttributeError("BankAccount instance has no attribute 'deposit'"
答案 0 :(得分:0)
如果您的缩进实际上是您发布的,那么deposit
函数是在__init__
方法中定义的。因此,它不是该类的属性,而是__init__
方法中仅提供的功能。以下代码只是代码的副本,但修复了缩进:
class BankAccount:
def __init__(self,balance=0):
self.balance = balance
def deposit(self, deposit_amount=30):
self.deposit_amount=deposit_amount
self.balance += deposit_amount
return self.balance
def withdraw(self,withdraw_amount=10):
if withdraw_amount > self.balance:
raise RuntimeError('Invalid Transaction')
self.balance -= withdraw_amount
return self.balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
此代码适用于我,就像我用c = BankAccount()
c = MinimumBalanceAccount()
时一样