是否有不同的方法来定义不是类初始创建的类?

2024-09-30 02:17:08 发布

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

我不确定我做错了什么。我对代码进行了多次检查,但我不明白它为什么不起作用。我想当你创建一个类的时候,你不需要在创建它的实例时“定义”它吗

代码如下:

class BankAccount:
    #init method with 3 parameters(self, balance & name)
    def __init__(self, balance, name):
       
       #variables and their assignments
       self.balance = balance
       self.name = name
        
       num_deposits = 0
       deposit_total = 0
       num_withdraws = 0
       withdraw_total = 0
     
    #numDeposit method with 1 parameter, itself.
    def numDeposits(self):
        
        #refer to num_deposits instance and increment by 1
        num_deposit += 1
        
    #'numWithdraw' method
    def numWithdraws(self):
        
        #update instance
        num_withdraws += 1
        
    #method named 'Deposit" that takes in 2 parameters(self & amount)
    def Deposit(self, amount):
        
        #update balance instance by (amount)
        balance += amount
        
        #update deposit_total instance by amount
        deposit_total += amount
        
        #calling numDeposit method to update number of total deposits
        numDeposits()
        
    #method named 'withdraw' that takes in 2 parameters(self & amount)
    def withdraw (self, amount):
        
        #updating instance variables 'balance' and 'withdraw_total'
        balance -= 1
        withdraw_total += 1
        
        #calling 'numWithdraws' method
        numWithdraws()
        
    # 'endOfMonth' method that has 1 parameter (self)
    def endOfMonth(self):
        
        #print statements
        print("Bank account: ", name)
        print("Balance : ", balance)
        print("Number of deposits: ", num_deposit, "totalling ", deposit_total)
        print("Number of withdraws: ", num_withdraws, "totalling ", withdraw_total)
        
    # 2 instances of BankAccount
    BankAccount1 = BankAccount( 0 , "chase" )
    BankAccount2 = BankAccount( 100, "Bank of America")
    
    #invoking deposit and withdraw for instance 1
    BankAccount1.deposit(50)
    BankAccount1.deposit(50)
    BankAccount1.withdraw(100)
    BankAccount1.withdraw(100)
    BankAccount1.withdraw(100)
    #calling endOfMonth method for instance 1
    BankAccount1.endOfMonth()
    
    #invoking deposit and withdraw for instance 2
    BankAccount2.deposit(25)
    BankAccount2.deposit(25)
    BankAcconut2.deposit(5000)
    BankAccount2.withdraw(10)
    BankAccount2.withdraw(1000)
    BankAccount2.withdraw(70)
    #calling endOfMOnth method for instance 2
    BankAccount2.endOfMonth()

每次运行代码时,我都会收到一个NameError,它表示“BankAccount”的名称没有定义? 我期望的结果是:

银行帐户:大通 结余:$-200 仓库数量:2总计$100 取款次数:3次共300美元

银行账户的第二个实例也是如此


Tags: andinstancenameselfdefamountnummethod
3条回答

根据缩进,看起来您正在创建BankAccount1和2,并在BankAccount类的定义中使用它们?在该命名空间中,BankAccount不存在

创建的bankAccount实例仍然缩进到类中。这使得bankAccount调用在类创建中完成。由于该类在创建过程中尚不存在,因此会引发NameError。因此,你应该取消对他们的影响。如果您真的想将它们设置为类atributes,那么应该使用bankAccount.BankAccount1=...unindented

需要明确的是:当一个类不是该类的实例时,仍然可以在该类中设置类属性

您在缩进和语法方面有一些问题

使用类属性时,如果要更改该类实例的属性或使用方法,则需要使用self.前缀:

def numDeposits(self):
        
    num_deposits += 1    # this just alters an undefined variable called num_deposits
    self.num_deposits += 1    # this is what you need

其次,您的属性需要在任何方法的范围之外进行定义,以便按照您希望的方式工作

还有一些其他方面可以改进,比如docstring是描述性的,但不是冗余的,或者使用它们没有任何意义(带有3个参数的init方法是自解释的)


class BankAccount:

    num_deposits = 0
    deposit_total = 0
    num_withdraws = 0
    withdraw_total = 0
    #init method with 3 parameters(self, balance & name)

    def __init__(self, balance, name):
       #variables and their assignments
       self.balance = balance
       self.name = name
        
     
    #numDeposit method with 1 parameter, itself.
    def numDeposits(self):
        
        #refer to num_deposits instance and increment by 1
        self.num_deposits += 1
        
    #'numWithdraw' method
    def numWithdraws(self):
        
        #update instance
        self.num_withdraws += 1
        
    #method named 'Deposit" that takes in 2 parameters(self & amount)
    def deposit(self, amount):
        
        #update balance instance by (amount)
        self.balance += amount
        
        #update deposit_total instance by amount
        self.deposit_total += amount
        
        #calling numDeposit method to update number of total deposits
        self.numDeposits()
        
    #method named 'withdraw' that takes in 2 parameters(self & amount)
    def withdraw (self, amount):
        
        #updating instance variables 'balance' and 'withdraw_total'
        self.balance -= 1
        self.withdraw_total += 1
        
        #calling 'numWithdraws' method
        self.numWithdraws()
        
    # 'endOfMonth' method that has 1 parameter (self)
    def endOfMonth(self):
        
        #print statements
        print("Bank account: ", self.name)
        print("Balance : ", self.balance)
        print("Number of deposits: ", self.num_deposits, "totalling ", self.deposit_total)
        print("Number of withdraws: ", self.num_withdraws, "totalling ", self.withdraw_total)
        
# 2 instances of BankAccount
BankAccount1 = BankAccount( 0 , "chase" )
BankAccount2 = BankAccount( 100, "Bank of America")

#invoking deposit and withdraw for instance 1
BankAccount1.deposit(50)
BankAccount1.deposit(50)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
#calling endOfMonth method for instance 1
BankAccount1.endOfMonth()

#invoking deposit and withdraw for instance 2
BankAccount2.deposit(25)
BankAccount2.deposit(25)
BankAccount2.deposit(5000)
BankAccount2.withdraw(10)
BankAccount2.withdraw(1000)
BankAccount2.withdraw(70)
#calling endOfMOnth method for instance 2
BankAccount2.endOfMonth()

相关问题 更多 >

    热门问题