【发布时间】:2018-11-14 17:37:47
【问题描述】:
我最近开始使用 Python 学习 OOP,为了测试我的类构建和继承技能,我为银行应用程序构建了一个简单的框架。据我所知,根据我对如何在 OOP 中执行继承的了解,我的代码语法没有问题:
import pickle, sys, datetime
class Bank():
def __init__(self):
with open('accounts.txt','rb') as fp:
accounts=pickle.load(fp)
def exit(self):
with open('accounts.txt','wb') as fp:
pickle.dump(accounts,fp)
sys.exit()
class TransactionMenu(Bank):
def __str__(self,account):
if str(super().accounts[account])[::-1].find('.')==-1:
nT=str(super().accounts[account])+'.00'
elif str(super().accounts[account])[::-1].find('.')==1:
nT=str(super().accounts[account])+'0'
else:
nT=str(super().accounts[account])
return '£'+nT
def balanceEnquiry(self,account):
print('Date: '+datetime.datetime.now()+'\n'+'Account name: '+account+'\n'+'Balance: '+self.__str__(account))
def deposit(self,account):
while True:
print('current balance: '+self.__str__(account))
amt=input('enter deposit amount: ')
try:
if '.' in amt:
amt=float(amt)
else:
amt=int(amt)
super().accounts[account]+=amt
print('new balance: '+self.__str__(account))
break
except:
pass
def withdraw(self,account):
while True:
print('current balance: '+self.__str__(account))
amt=input('enter withdrawal amount: ')
try:
if '.' in amt:
amt=float(amt)
else:
amt=int(amt)
super().accounts[account]-=amt
print('new balance: '+self.__str__(account))
break
except:
pass
def __init__(self,account):
while True:
choice=input('enter balance enquiry, deposit, withdraw or exit: ').lower()
if choice=='b' or choice=='balance' or choice=='balance enquiry' or choice=='enquiry' or choice=='balance enquiry' or choice=='bal':
self.balanceEnquiry(account)
elif choice=='d' or choice=='deposit':
self.deposit(account)
elif choice=='w' or choice=='withdraw':
self.withdraw(account)
elif acc=='e' or acc=='exit':
super().exit()
class Menu(Bank):
def logIn(self):
while True:
accN=input('enter your account name or exit: ').lower()
if acc=='e' or acc=='exit':
super().exit()
else:
for account in super().accounts:
if accN==account:
TransactionMenu(accN)
break
def createAccount(self):
while True:
newAcc=input('enter your forename and surname or exit: ').lower()
if acc=='e' or acc=='exit':
super().exit()
else:
super().accounts[newAcc]=0.00
def __init__(self):
while True:
acc=input('enter login, register or exit: ').lower()
if acc=='l' or acc=='login' or acc=='log in' or acc=='log-in':
self.logIn()
break
elif acc=='r' or acc=='register' or acc=='reg':
self.createAccount()
break
elif acc=='e' or acc=='exit':
super().exit()
Bank().Menu()
但是,当我调用 Bank().Menu() 时,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\xavier\Desktop\bank\bank.py", line 104, in <module>
Bank().Menu()
AttributeError: 'Bank' object has no attribute 'Menu'
这意味着我没有正确执行我的继承,但是我看不出这是怎么回事。
非常感谢任何帮助。
【问题讨论】:
-
您可能会发现研究继承和组合之间的区别很有用。通常,继承意味着“是”关系。因此,由于
TransactionMenu继承自Bank,因此您是说每个TransactionMenu都是一个Bank。组合是一种“具有”关系。所以如果你定义Bank.transactionmenu = TransactionMenu(),那么每个Bank都有一个TransactionMenu。通过继承和组合来关联两个类几乎是不合适的。 -
我不清楚你为什么认为
Bank().Menu()会起作用,或者你期望它做什么。 Menu 没有嵌套在 Bank 中,或者是它的一个属性。我不确定继承在这里是否有意义,例如,菜单真的是一种更具体的银行吗?
标签: python-3.x oop inheritance