如何在只有self参数的构造函数中设置属性?我也不知道如何定义没有实现的方法,在一个类里面



这就是问题:它让我大吃一惊;我的尝试是在我的下一个帖子。

创建一个名为bank account的类,该类具有withdraw和deposit方法,但没有实现。

创建一个名为savings account的类,它继承自银行帐户。SavingsAccount应该有一个只接受self参数的构造函数。此构造函数将名为balance的属性设置为500。(这应该是在任何给定时间的最小余额)。

在储蓄账户类中,实现存款方法,该方法接受现金存款金额,相应地更新余额,然后返回余额。对于负存款金额,退回无效存款金额。在储蓄账户类中,实现提取方法,该方法接受现金提取金额,从当前余额中扣除该金额并返回余额。这种方法不应该让余额低于500。(检查此情况并输出,如果出现此情况则不能超过最低账户余额取款)。如果提现金额大于活期余额,则不能超过活期余额提现。对于负数的提款金额,返回Invalid withdraw amount。

创建一个名为current account的类,它继承自bank account。CurrentAccount应该有一个构造函数,该构造函数只接受self参数,并将一个名为balance的属性设置为0。

在current account类中,实现一个存款方法,该方法接受现金存款金额,相应地更新余额,然后返回余额。存款额为负的,退回无效的存款额。在活期账户类中,实现一个提取方法,该方法接受现金提取金额,从活期余额中扣除该金额并返回余额。对于负数的提现金额,返回无效的提现金额。提现超过当前账户余额将失败,提示不能超过当前账户余额提现。

这是我的尝试:

class BankAccount:
        def __init__ 
(self,name,number,balance):
            self.name=name
            self.number=number
            self.balance=balance
class SavingsAccount(BankAccount):
      def __init__(self, balance=500):
      assert(self.balance>500), "minimum balance is 500"
    def deposit (self, amount):
        if amount<0:
            return "Invalid deposit amount"
        self.balance+=amount
        return self.balance
    def withdraw(self,amount):

        Assert
        (self.balance>amount),
        "cannot withdraw" 
        if amount<0:
            return "Invalid amount"
        self.balance-=amount
        return self.balance
class currentaccount(BankAccount):
    def __init__(self,balance=0):
        def deposit (self, amount):
            if amount<0:
                return ("Invalid deposit amount")
            self.balance+=amount
            return self.balance
    def withdrawal (self, amount):
        assert (self.balance > amount),"cannot withdraw beyond the current account balance"
        if amount<0:
            return "Invalid amount"
        self.balance-=amount
        return self.balance

下面的代码可以工作!

class BankAccount(object):
    def __init__(self):
        pass
    def withdraw():
        pass
    def deposit():
        pass
class SavingsAccount(BankAccount):
    def __init__(self):
        self.balance = 500
    
    def deposit(self, amount):
        if (amount < 0):
            return "Invalid deposit amount"
        else:
            self.balance += amount
            return self.balance
    
    def withdraw(self, amount):
        if ((self.balance - amount) > 0) and ((self.balance - amount) < 500):
            return "Cannot withdraw beyond the minimum account balance"
        elif (self.balance - amount) < 0:
            return "Cannot withdraw beyond the current account balance"
        elif amount < 0:
            return "Invalid withdraw amount"
        else:
            self.balance -= amount
            return self.balance 
class CurrentAccount(BankAccount):
    def __init__(self):
        self.balance = 0
    
    def deposit(self, amount):
        if amount < 0:
            return "Invalid deposit amount"
        else:
            self.balance += amount
            return self.balance
    
       
    def withdraw(self, amount):
        if amount < 0:
            return "Invalid withdraw amount"
        elif self.balance < amount:
            return "Cannot withdraw beyond the current account balance"
        else:
            self.balance -= amount
            return self.balance
    

仔细阅读分配:每次设置balance为500;不需要另一个构造函数参数。没有名字,没有账号。

"no implementation"方法很简单:

def method():
    pass

这会让你动起来吗?不要试图让它变得比实际更困难:从简单开始,每次写几行,然后在继续之前调试它们。

要设置一个只有self参数的构造函数,请执行以下操作

class Example(object):
    def __init__(self):
        pass

对于这个特定的问题,一步一步地阅读说明,并按照说明执行方法。这里的问题是要有正确的逻辑。在储蓄账户类中,确保首先检查透支情况,然后检查取款不会导致余额少于500。这个顺序很重要。我也遇到过类似的问题,我在做了这个修改后解决了它。

解决方案如下:

class BankAccount(object):
    def withdraw():
        pass
    def deposit():
        pass

class SavingsAccount(BankAccount):
    def __init__(self):
        self.balance = 500
    def deposit(self, amount):
        if amount < 0:
            return "Invalid deposit amount"
        else:
            self.balance += amount
        return self.balance
    def withdraw(self, amount):
        if amount < 0:
            return "Invalid withdraw amount"
        elif amount > self.balance:
            return "Cannot withdraw beyond the current account balance"
        elif (self.balance - amount) < 500:
            return "Cannot withdraw beyond the minimum account balance"
        else:
            self.balance -= amount
        return self.balance

class CurrentAccount(BankAccount):
    def __init__(self):
        self.balance = 0
    def deposit(self, amount):
        if amount < 0:
            return "Invalid deposit amount"
        else:
            self.balance += amount
        return self.balance
    def withdraw(self, amount):
        if amount < 0:
            return "Invalid withdraw amount"
        elif self.balance < amount:
            return "Cannot withdraw beyond the current account balance"
        else:
            self.balance -= amount

最新更新