有没有其他方法来定义一个不是类的初始创建的类?



我不知道我做错了什么。我把代码看了很多遍,我不明白为什么它不工作。我想当你创建一个类,那么你不必"定义"它时,创建它的实例?

代码如下:

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

对于Bank Account 的第二个实例,等等

你有一些缩进问题,还有语法。

当你使用类属性时,如果你想改变类实例的属性,或者使用一个方法,你需要使用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

其次,你需要在任何方法的作用域之外定义属性,以使它们按照你想要的方式工作。

还有一些你可以改进的东西,比如文档字符串是描述性的,但不是多余的,或者使用它们没有意义(带有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()

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

创建的bankAccount实例仍然在类中缩进。这使得其中的bankAccount调用在类创建时完成。由于类在创建过程中还不存在,因此会引发NameError。因此,您应该取消缩进。如果你真的想把它们设置为类属性,你应该使用无缩进的bankAccount.BankAccount1=...

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

相关内容

  • 没有找到相关文章

最新更新