银行帐户类。如何使其更强大/更有效? (OOP)

时间:2013-11-19 16:30:07

标签: python oop python-2.7

我想在不使用全局变量的情况下使这个程序工作。基本上,如果此人退出并最终以负数计算,则会产生5的罚款。每次减负的交易也会导致5的罚款。

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

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

但这就是它的局限性。我如何允许无限帐户?所以我不会受限于两个全局变量。我希望这是有道理的,假设我必须改变撤销功能和get_fees,但我是OOP的新手所以我被困住了。谢谢你的帮助!

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

4 个答案:

答案 0 :(得分:5)

将惩罚设为实例属性,就像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

答案 1 :(得分:4)

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

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

我还注意到你在penalty函数中声明了一个名为withdraw的变量。这似乎是避免magic numbers的良好实践的开始,所以我继续沿着这些方向使它成为BankAccount类的常量属性。

此外,在Python中,我们通常不仅仅使用函数来访问属性。不是bobs_account.get_fees()而是bobs_account.penalty更为正常。

答案 2 :(得分:3)

惩罚也应该是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

答案 3 :(得分:0)

class BankAccount:

 accounts = {}
 def __init__(self,account_id, initial_balance):
       BankAccount.accounts[account_id] = self
       ...

 @classmethod
 def get_account(cls,acct_id):
      return BankAccount.accounts.get(acct_id)

这样的事可能

BankAccount(1,20)
print BankAccount.get_account(1)
BankAccount("apple",100)
print BankAccount.get_account("apple")

这是解决多个帐户的一种方法,遵循有关惩罚的其他建议