【发布时间】:2020-12-18 13:05:12
【问题描述】:
我是python开发的新手 我试图访问基类中的父类属性,但它给了我错误
Traceback(最近一次调用最后一次): 第 41 行,在 account.getAccountInfo() 第 24 行,在 getAccountInfo 中 print("ID" + str(self.cust.custID)) AttributeError: 'Account' 对象没有属性 'cust'
这是我的代码
class Customer:
def __int__(self, custID=00000, cust_name="IJK", address="CABI", contact=9090909090):
self.custID = custID
self.cust_name = cust_name
self.address = address
self.contact = contact
class Account(Customer):
def __int__(self, accountID=89839832389, balance=89999999.99):
self.cust = super.__int__(self)
self.accountID = accountID
self.balance = balance
def getAccountInfo(self):
print("ID " + str(self.cust.custID))
print("Name " + str(self.cust.cust_name))
print("Account ID " + str(self.cust.accountID))
def deposit(self, balance=2000, savings="true"):
return self.balance + balance
def withdraw(self, balance=500):
return self.balance - balance
def getBalance(self):
return self.balance
account = Account()
account.getAccountInfo()
print("Actual balance " + str(account.getBalance()))
account.deposit()
print("Balance after deposit " + str(account.getBalance()))
account.withdraw()
print("Balance after withdraw " + str(account.getBalance()))
【问题讨论】:
-
1)
__int__vs__init__2)self.cust = super.__int__(self)init 方法不返回任何内容 3) 不确定您是尝试继承还是只需要在 Account 中创建 Customer 对象 -
Himaprason 我必须做这件事创建一个从银行类继承的帐户类,具有以下属性(使用 Super() 将值传递给基类):
标签: python inheritance