class Account:
def __init__(self, initial):
self.balance = initial
def deposit(self, amt):
self.balance = self.balance + amt
def withdraw(self,amt):
self.balance = self.balance - amt
def getbalance(self):
return self.balance
a = Account(1000.00)
a.deposit(550.23)
a.deposit(100)
a.withdraw(50)
print a.getbalance()
Run Code Online (Sandbox Code Playgroud)
运行此代码时出现此错误.NameError:帐户实例没有属性"存款"
class Account:
def __init__(self, initial):
self.balance = initial
def deposit(self, amt):
self.balance = self.balance + amt
def withdraw(self,amt):
self.balance = self.balance - amt
def getbalance(self):
return self.balance
Run Code Online (Sandbox Code Playgroud)
你定义它们的方式,它们是__init__方法的本地,因此是无用的.
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |