如何在两个对象之间加减值?我有一个任务,我需要做一些银行帐户作为一个对象。在他们之间转账。你知道吗
class Account():
def __init__(self, name, balance, number):
self.name = name
self.balance = balance
self.number = number
def transfer(self, amount):
self.amount = amount
if self.balance >= amount:
self.balance = self.balance - amount
else:
print("Sorry but you do not have enough funds on your account")
def add(self, add):
self.add = add
self.balance = self.balance + add
def show(self):
print("Hello", self.name, "your current balance is:", self.balance)
acc_1 = Account("Darren William", 2000.50, 3694586)
acc_2 = Account("Jamie Lanister", 7500.34, 3687756)
acc_1.show()
acc_1.transfer(300.89)
acc_1.show()
acc_1.add(500.47)
acc_1.show()
所以现在我做了一些方法,从一个物体上加上和减去x的钱。但我怎样才能把它和账户联系起来。举个例子,如果我从acc1减去100,再加100到acc2。你知道吗
我建议上两节课。一个是客户本身,另一个是客户经理,类似这样:
因此,现在您可以更好地控制您的帐户,并为每个帐户设置单独的逻辑。当然,如果你想更好地控制账户,你可以改进这一点,也许可以使用字典。你知道吗
希望有帮助!你知道吗
如果您有两种方法,分别用于转账记入贷方的帐户和转账到记入借方的帐户,则该程序将起作用。你知道吗
我不能保证添加下面的代码是最科学的实现,但它可以按预期工作。你知道吗
相关问题 更多 >
编程相关推荐