【发布时间】:2014-12-07 11:32:06
【问题描述】:
我已经学习 Python 作为我的第一语言大约两周了,我喜欢它。我一直在使用 Learn Python the Hard way,但是当我遇到面向对象编程时,我的大脑几乎要爆炸了。我做了很多小时的研究,我以为我终于明白了要点,但现在我有点卡住了。
我创建了一个非常简单的银行程序,尝试使用类。我做得很好,直到我遇到了一个大问题。事实上,它可以工作(为简洁起见,我没有发布菜单设置,但只要我只有这三个对象,它就可以满足我的需求。)问题就在这里。
- 问题:如果有多个实例,如何操作实例值。 TLDR:我怎样才能不对对象引用进行硬编码? 请查看我的主要 BankAccount 类中的 Transfer 函数:我在对象(帐户)saccount.balance 和 paccount.balance 变量中进行了硬编码,但是如果有许多不同的帐户怎么办?我如何才能编辑他们的余额,也就是转账?
如何使 Transfer() 方法正确引用它们需要转到的实例?我这样问对吗?
我是否错误地使用了 OOP?
如果有多个用户或多个银行帐户怎么办?像“daccount”、“faccount”等我将如何管理他们的余额和转账?
请温柔一点……
这是我的主要课程:
class BankAccount:
#### NO CLASS VARIABLES
def __init__(self):
self.balance = 500 #This is an instance variable
def withdraw(self, amount):
self.balance = self.balance - amount
print "You withdrew %d dollars\n" % amount
return self.balance
def deposit(self, amount):
self.balance += amount
print "You deposited %d dollars\n" % amount
return self.balance
def transfer(self, amount): ### Here is our problem
print "Where would you like to transfer money from?"
answer = raw_input("1.) From CHECKINGS to SAVINGS\n2.)From SAVINGS to CHECKINGS\n >>>")
if answer == "1":
baccount.balance -= amount #What would I do if there were many accounts?
saccount.balance += amount #I originally tried this as SavingsAccount.balance, but that would be a "Class Variable" correct?
elif answer == "2":
saccount.balance -= amount
baccount.balance += amount
else:
menu()**
def printbal(self):
print "Your balance is currently %d dollars." % self.balance
return self.balance
这是我的两个子类(最低余额检查和储蓄)
class MinBalAccount(BankAccount): #This is a BankAccount and has a MinBal
def __init__(self, minbalance): #This sets our minbalance during 'instantation'
BankAccount.__init__(self) #This gives us the variable self.balance
print "ATM INITIALIZED. WELCOME TO SKYNET BANK"
self.minbalance = minbalance #Sets the minimum balance for this minbalaccount
def withdraw(self, amount):
while self.balance - amount < self.minbalance: #THis allows for a loop,
print "Apologies, you must maintain a balance of 1.00"
print "If you withdraw %d from your current balance of %d it will leave you with a balance of %d dollars." % (amount, self.balance, self.balance - amount)
print "Please Re-Enter The AMOUNT You would like to withdraw"
amount = int(raw_input("\nAmount:"))
BankAccount.withdraw(self, amount)
self.printbal() #We can do this because we inherited it from out parent class. We could also write it as MinBalAccount.printbal(self) or BankAccount.printbal(self)
class SavingsAccount(BankAccount):
def __init__(self,minbalance,balance):
self.minbalance = minbalance
self.balance = balance
paccount = BankAccount()
baccount = MinBalAccount(1.00)
saccount = SavingsAccount(300, 500)
【问题讨论】:
-
如果您有很多 BankAccount 实例并且您想对所有这些实例进行 transfer(),您是否想知道该怎么做?
-
与其全部转账...假设我有 'paccount = BankAccount() baccount = MinBalAccount() saccount = SavingsAccount iaccount = InvestingAccount()(我会创建一个菜单项“打开”并实例化一个 InvestingAccount 类对象)'我现在有一个额外的类实例(投资账户),我的转移方法无法处理。我如何才能允许我的用户对该帐户进行操作?
标签: python oop python-2.7 object instance