银行账户类别。如何使它更强大/更有效率?(哎呀)

2024-10-01 04:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让这个程序在不使用全局变量的情况下运行。基本上,如果此人退出并最终以负数结束,他们将被处以5英镑的罚款。每笔交易如果是负的,也会被罚5英镑。

假设只有两个帐户,这个程序似乎运行良好:

account1 = Bankaccount(20)
account2 = Bankaccount(5)

但这是它的局限性。如何允许无限帐户?所以我不必拘泥于两个全球性的。我希望这是有意义的,假设我必须改变取款功能并获得费用,但我是新的面向对象编程,所以我卡住了。谢谢你的帮助!

pen = 0
pen2 = 0


class BankAccount:


 def __init__(self, initial_balance):
        """Creates an account with the given balance."""
        self.money = initial_balance

    def deposit(self, amount):
        """Deposits the amount into the account."""
        self.money += amount
        return self.money

    def withdraw(self, amount):
        """
        Withdraws the amount from the account.  Each withdrawal resulting in a
        negative balance also deducts a penalty fee of 5 dollars from the balance.
        """
        global pen, pen2
        penalty = 5

        if self.money - amount < 0:
            self.money -= (amount + penalty)
            if self == account1:
                pen += 5
            elif self == account2:
                pen2 += 5
        else:
            self.money -= amount
        return self.money


    def get_balance(self):
        """Returns the current balance in the account."""
        return self.money

    def get_fees(self):
        """Returns the total fees ever deducted from the account."""
        global pen, pen2
        if self == account1:
            return pen
        elif self == account2:
            return pen2

Tags: thefromselfreturnifdefaccountamount
3条回答

将惩罚设置为实例属性,就像money是:

def __init__(self, initial_balance):
        """Creates an account with the given balance."""
        self.money = initial_balance
        self.penalty = 0

后来:

def withdraw(self, amount):

    if self.money - amount < 0:
        self.penalty += 5

只需将全局变量转换为实例变量:

class BankAccount:
    PENALTY = 5
    def __init__(self, initial_balance):
        """Creates an account with the given balance."""
        self.money = initial_balance
        self.penalty = 0

    def deposit(self, amount):
        """Deposits the amount into the account."""
        self.money += amount
        return self.money

    def withdraw(self, amount):
        """
        Withdraws the amount from the account.  Each withdrawal resulting in a
        negative balance also deducts a penalty fee of 5 dollars from the balance.
        """
        if self.money - amount < 0:
            self.money -= (amount + BankAccount.PENALTY)
            self.penalty += BankAccount.PENALTY
        else:
            self.money -= amount
        return self.money


    def get_balance(self):
        """Returns the current balance in the account."""
        return self.money

    def get_fees(self):
        """Returns the total fees ever deducted from the account."""
        return self.penalty

我还注意到您在withdraw函数中声明了一个名为penalty的变量。这看起来是避免magic numbers的良好实践的开始,因此我继续沿着这些思路,将其作为BankAccount类的常量属性。

另外,在Python中,我们通常不使用函数来访问属性。与其bobs_account.get_fees(),不如bobs_account.penalty更正常。

惩罚还应该是BankAccount对象的实例变量:

class BankAccount:
    penalty_amount = 5

    def __init__(self, initial_balance):
        self.money = initial_balance
        self.penalty = 0

    def withdraw(self, amount):
        """
        Withdraws the amount from the account.  Each withdrawal resulting in a
        negative balance also deducts a penalty fee of 5 dollars from the balance.
        """

        if self.money - amount < 0:
            self.money -= (amount + BankAccount.penalty_amount)
            self.penalty += BankAccount.penalty_amount

        else:
            self.money -= amount
        return self.money

相关问题 更多 >