因此,我的课程彼此不认识,但当让他们单独工作时,他们会像文章底部的参考打印一样工作。然而,存储和传输会在ram位置弹出,但是,它们在单独完成时工作良好,正如底部的“咨询”操作所示
<;功能出纳。存款地址:0x0000023A9B656048>
import datetime
now = datetime.datetime.now()
class account:
owner = "name"
pin = "1823"
balance = 800
def __init__(self, transfer, withdraw, deposit, consult):
self.transfer = transfer
self.withdraw = withdraw
self.deposit = deposit
self.consult = consult
class cashier:
def __init__(self, withdraw, deposit, transfer, consult):
self.transfer = transfer
self.consult = consult
self.withdraw = withdraw
self.deposit = deposit
def deposit(self):
print("Please type pin to proceed")
if account.pin == 1823:
print("who would you like to send money to?")
else:
print("Invalid pin")
def transfer(self):
pass
def withdraw(self):
withdrawal = input("How much money do you want to withdraw? (there is a limit up to 10,000$ a day!)")
account.balance -= int(withdrawal)
if int(withdrawal) > 10000:
print("withdrawals cannot be larger than 10,000 a day!")
exit()
elif int(withdrawal) > account.balance:
print("your account does not have enough funds to complete your transaction")
else:
print("Transaction succesfull!\nPlease collect your money")
print('Today is', now)
print("Hello %s!\nWhat can I do for you today?" % account.owner)
action = input("Use commands such as withdraw, deposit, transfer or consult to continue!\n")
if action == "withdraw":
print(cashier.withdraw)
if action == "consult":
print("Your account's balance is %s $" % str(account.balance))
if action == "transfer":
print(cashier.transfer)
if action == "deposit":
print(cashier.deposit)
else:
print("Uknown command, exiting programm")
exit()
要使用类,需要像这样实例化它们
现在,您可以使用出纳变量上的函数,如:-
我认为,如果您不想实例化并且想要一个不实际存储数据的函数,您可以使用静态函数,但我不认为这是您想要做的
cashier
的各种函数不返回任何内容,因此打印只是打印函数的字符串表示形式。您可以让函数返回如下字符串:编辑:我删除了初始值设定项,因为它们什么都没做。您需要用
cashier = Cashier()
实例化出纳,然后用cashier.withdraw()
调用Cashier
函数这里对类的工作方式(以及OOP的一般工作方式)有一个基本的误解。在继续之前,你应该通读一遍Python docs on building classes
相关问题 更多 >
编程相关推荐