我正在编写一种实现银行帐户的方法。很简单,我希望输出的是用户的姓名和帐户类型。但是,我在主课中使用时遇到了问题Enum。
from enum import Enum
class AccountType(Enum):
SAVINGS = 1
CHECKING = 2
#bank account classes that uses AccountType
class BankAccount():
def __init__(self, owner, accountType):
self.owner = owner
self.accountType = accountType
def __str__(self):
self.d = AccountType(1)
return "The owner of this account is {} and his account type is: {} ".format(self.owner, self.d)
#test the code
test = BankAccount("Max", 1)
print(test)
输出
The owner of this account is Max and his account type is: AccountType.SAVINGS
所以这是所需的输出,但这仅在我对__str__方法 ( AccountType(1)) 中的帐户类型进行硬编码时才有效。为了澄清,我的意思是这一行:
BankAccount("Max", 1)
有没有办法做到这一点,如果我输入accountType1的BankAccount参数,它会返回
The owner of this account is Max and his account type is: AccountType.SAVINGS
湖上湖
有只小跳蛙
相关分类