Python 我需要帮助了解为什么继承不是';在这个OOP示例中不起作用
当我运行此程序时,出现错误“AttributeError:'SavingsAccount'对象没有属性“\u SavingsAccount\u\u balance”“”,我无法找出原因。错误链接到savings.draw()方法。欢迎任何帮助。很明显,我是OOP新手Python 我需要帮助了解为什么继承不是';在这个OOP示例中不起作用,python,oop,inheritance,Python,Oop,Inheritance,当我运行此程序时,出现错误“AttributeError:'SavingsAccount'对象没有属性“\u SavingsAccount\u\u balance”“”,我无法找出原因。错误链接到savings.draw()方法。欢迎任何帮助。很明显,我是OOP新手 import random class Account: def __init__(self,holderName,balance): # generate a 4-digit PIN se
import random
class Account:
def __init__(self,holderName,balance):
# generate a 4-digit PIN
self.__PIN=''
for x in range(4):
self.__PIN+=chr(random.randint(48,57))
# generate an 8-digit account number
self.__accountNumber=''
for x in range(8):
self.__accountNumber+=chr(random.randint(48,57))
self.__holderName = holderName
self.__balance = balance
def deposit(self,amount):
self.__balance+=amount
def getBalance(self):
return(self.__balance)
def setBalance(self,amount):
self.__balance = amount
def withdraw(self, amount):
self.__balance -= amount
def setAccountHolder(self,name):
self.__holderName = name
def getAccountHolder(self):
return self.__holderName
def PrintAccountInfo(self):
print('\nPIN: {}'.format(self.__PIN))
print('Account Number: {}'.format(self.__accountNumber))
print('Account Holder: {}'.format(self.__holderName))
print('Balance: £{:.2f}'.format(self.__balance))
class SavingsAccount(Account):
def __init__(self, holderName, balance):
super().__init__(holderName, balance)
self.__withdrawals = 0
def withdraw(self, amount):
if self.__withdrawals < 4:
if self.__balance - amount >= 0.00:
self.__balance -= amount
self.__withdrawals += 1
else:
print('Cannot withdraw - insufficient funds.')
else:
print('You have exceeded the number of withdrawals for this session')
def PrintAccountInfo(self):
super().PrintAccountInfo()
print('Withdrawals: {}'.format(self.__withdrawals))
# main program
main = Account('Joe Smith',400)
main.deposit(100)
main.PrintAccountInfo()
savings = SavingsAccount('Ann Brown',400)
savings.PrintAccountInfo()
savings.withdraw(200)
随机导入
类别帐户:
定义初始(自身、持有者名称、余额):
#生成一个4位PIN
自我。u_引脚=“”
对于范围(4)内的x:
self._PIN+=chr(random.randint(48,57))
#生成一个8位数的帐号
self.\uuu accountNumber=“”
对于范围(8)内的x:
self._accountNumber+=chr(random.randint(48,57))
self.\uu holderName=holderName
自平衡=平衡
def保证金(自身、金额):
自身余额+=金额
def getBalance(自平衡):
返回(自平衡)
def设置余额(自身、金额):
自身余额=金额
def提取(自身、金额):
自身余额-=金额
def setAccountHolder(自我,姓名):
self.\uuu holderName=名称
def getAccountHolder(自身):
返回自我
def PrintAccountInfo(自我):
打印('\n输入:{}.格式(self.\u-PIN))
打印('accountNumber:{}'。格式(self.\u accountNumber))
打印('accountholder:{}'。格式(self.\uu holderName))
打印('Balance:£{.2f}'。格式(self.\uu Balance))
类别储蓄账户(账户):
定义初始(自身、持有者名称、余额):
super()。\uuuuu init\uuuuuu(保持名称,平衡)
自提款=0
def提取(自身、金额):
如果自提款<4:
如果自身余额-金额>=0.00:
自身余额-=金额
自提款+=1
其他:
打印('无法提取-资金不足')
其他:
打印('您已超过此会话的取款次数')
def PrintAccountInfo(自我):
super().PrintAccountInfo()
打印('取款:{}'。格式(自取款))
#主程序
main=账户('Joe Smith',400)
主要存款(100)
main.PrintAccountInfo()
储蓄=储蓄账户('Ann Brown',400)
savings.PrintAccountInfo()
储蓄.提取(200)
Addpython
tag。几乎肯定会有一个重复的标签,但是请阅读文档中的相关内容。更常见的是,只使用带有一个前缀的名称来表示私有属性。Name mangling专门用于对子类隐藏名称。谢谢。我还没有完全理解Python中的私有属性。我将使用getter方法,而不是直接从子类使用self.\u balance。