python嵌套类

python嵌套类,第1张

python嵌套

我的代码版本,带有注释

## 1. CamelCasing for classes#class Account:    def __init__(self):        # 2. to refer to the inner class, you must use self.Bank        # 3. no need to use an inner class here        self.bank = self.Bank()    class Bank:        def __init__(self): self.balance = 100000        # 4. in your original pre, you had a method with the same name as         #    the attribute you set in the constructor. That meant that the         #    method was replaced with a value every time the constructor was         #    called. No need for a method to do a simple attribute lookup. This        #    is Python, not Java.        def withdraw(self, amount): self.balance -= amount        def deposit(self, amount): self.balance += amounta = Account()print(a.bank.balance)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642973.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存