我正在尝试使用聚合,但我想我弄错了
I'm trying to use aggregation but I think I get it wrong
所以我写了一个代码,银行 class 有一个验证方法和一个 SavingAccount class 有一个提取方法,我试图使用聚合来关联它们,但它不起作用。验证正在验证一切。重点应该放在银行 class 和储蓄账户 class 上,最后我放了不应该工作的实例 (acc1.withdraw()),我把其他的 class 以便您可以试用代码,无论如何这里是代码:
预期行为:如果不调用 add_acc() 函数,验证应该 return False 并且它不应该让我使用 withdraw( ) 函数。
实际行为:即使 add_acc() 未被调用,我也能够撤回()
from abc import ABC, abstractmethod
class Account(ABC):
def __init__(self, agency, acc_number, balance):
self.agency = agency
self.acc_number = acc_number
self.balance = balance
@abstractmethod
def withdraw(self, value):
pass
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Bank:
def __init__(self):
self.account = {}
def add_acc(self, client, account):
self.account.update({f'{client}': account})
def validation(self):
if self.account:
for acc in self.account:
if acc in self.account:
return True
else:
return False
class SavingAccount(Account):
if Bank.validation:
def withdraw(self, value):
if self.balance < value:
print('Insufficient funds.')
return
self.balance -= value
print(f'{value} dollars withdrawn. Current balance: {self.balance}$')
else:
print('Account information invalid.')
class Client(Person):
def __init__(self, name, age, acc_type):
super().__init__(name, age)
self.acc_type = acc_type
bank1 = Bank()
acc1 = SavingAccount(33333, 33330, 2000)
client1 = Client('Matthew', 40, acc1)
acc1.withdraw(500) # Right here this shouldn't work without me adding bank1.add_acc()
您的代码存在两个主要问题。
首先是您对 SavingsAccount
的实施存在严重缺陷。您正在检查 Bank.validate
是否真实,而不是调用它。但是,即使您确实调用了它,在您调用的位置它也没有任何意义。您正试图在定义 class 时 进行验证 ,而不是在您创建 class 的实例或尝试提取资金时进行验证。那没有任何意义。储蓄账户的概念(即 class 的定义)应该能够存在,即使还没有任何银行成立。稍后再做验证!并且您可能需要使用 Bank
class 的 某些特定实例 进行验证,而不是直接使用 Bank
class。
第二个问题是您的 Bank.validate
方法没有做任何有用的事情。它遍历 self.accounts
字典中的所有键,然后只检查第一个键以查看它是否在字典中(如果您到达代码的那部分,它总是会),然后 returns。可能您希望该功能检查 一个特定帐户 ,而不是检查任意帐户的一般情况。该帐户(或帐号或其他内容)可能应该是该函数的参数。
所以我写了一个代码,银行 class 有一个验证方法和一个 SavingAccount class 有一个提取方法,我试图使用聚合来关联它们,但它不起作用。验证正在验证一切。重点应该放在银行 class 和储蓄账户 class 上,最后我放了不应该工作的实例 (acc1.withdraw()),我把其他的 class 以便您可以试用代码,无论如何这里是代码:
预期行为:如果不调用 add_acc() 函数,验证应该 return False 并且它不应该让我使用 withdraw( ) 函数。
实际行为:即使 add_acc() 未被调用,我也能够撤回()
from abc import ABC, abstractmethod
class Account(ABC):
def __init__(self, agency, acc_number, balance):
self.agency = agency
self.acc_number = acc_number
self.balance = balance
@abstractmethod
def withdraw(self, value):
pass
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Bank:
def __init__(self):
self.account = {}
def add_acc(self, client, account):
self.account.update({f'{client}': account})
def validation(self):
if self.account:
for acc in self.account:
if acc in self.account:
return True
else:
return False
class SavingAccount(Account):
if Bank.validation:
def withdraw(self, value):
if self.balance < value:
print('Insufficient funds.')
return
self.balance -= value
print(f'{value} dollars withdrawn. Current balance: {self.balance}$')
else:
print('Account information invalid.')
class Client(Person):
def __init__(self, name, age, acc_type):
super().__init__(name, age)
self.acc_type = acc_type
bank1 = Bank()
acc1 = SavingAccount(33333, 33330, 2000)
client1 = Client('Matthew', 40, acc1)
acc1.withdraw(500) # Right here this shouldn't work without me adding bank1.add_acc()
您的代码存在两个主要问题。
首先是您对 SavingsAccount
的实施存在严重缺陷。您正在检查 Bank.validate
是否真实,而不是调用它。但是,即使您确实调用了它,在您调用的位置它也没有任何意义。您正试图在定义 class 时 进行验证 ,而不是在您创建 class 的实例或尝试提取资金时进行验证。那没有任何意义。储蓄账户的概念(即 class 的定义)应该能够存在,即使还没有任何银行成立。稍后再做验证!并且您可能需要使用 Bank
class 的 某些特定实例 进行验证,而不是直接使用 Bank
class。
第二个问题是您的 Bank.validate
方法没有做任何有用的事情。它遍历 self.accounts
字典中的所有键,然后只检查第一个键以查看它是否在字典中(如果您到达代码的那部分,它总是会),然后 returns。可能您希望该功能检查 一个特定帐户 ,而不是检查任意帐户的一般情况。该帐户(或帐号或其他内容)可能应该是该函数的参数。