【发布时间】:2014-03-24 05:15:12
【问题描述】:
我在某处读到__init__ 在创建对象时存储信息。所以,假设我有这个代码:
class BankAccount(object):
def __init__(self, deposit):
self.amount = deposit
def withdraw(self, amount):
self.amount -= amount
def deposit(self, amount):
self.amount += amount
def balance(self):
return self.amount
myAccount = BankAccount(16.20)
x = raw_input("What would you like to do?")
if x == "deposit":
myAccount.deposit(int(float(raw_input("How much would you like to deposit?"))))
print "Total balance is: ", myAccount.balance()
elif x == "withdraw":
myAccount.withdraw(int(float(raw_input("How much would you like to withdraw?"))))
print "Total balance is: ", myAccount.balance()
else:
print "Please choose 'withdraw' or 'deposit'. Thank you."
__init__ 在做什么或存储什么。我不明白什么是“self.amount”或者如何使它=存款做什么。 __init__下的“self.amount”和withdraw下的一样吗?我只是不明白“self_amount”是做什么的。
【问题讨论】:
-
这里已经解释过了:stackoverflow.com/questions/576169/…
-
@Christian 我目前一直在使用 learnpythonthehardway,我访问过很多网站并阅读了很多内容,但没有找到我的问题的答案。这就是我来这里的原因。
-
如果您对
self.amount所做的事情有误解,您应该尝试提出一个更简单的示例。试着想出一个你不懂的最简单的例子。
标签: python class object init self