當前位置:
首頁 >
python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数
發布時間:2023/11/30
40
豆豆
生活随笔
收集整理的這篇文章主要介紹了
python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這是我正在Python中做的:
class BaseClass:
def __init__(self):
print 'The base class constructor ran!'
self.__test = 42
class ChildClass(BaseClass):
def __init__(self):
print 'The child class constructor ran!'
BaseClass.__init__(self)
def doSomething(self):
print 'Test is: ', self.__test
test = ChildClass()
test.doSomething()
其結果如下:
AttributeError: ChildClass instance has no attribute '_ChildClass__test'
是什么賦予了?為什么不按照我的預期工作?
總結
以上是生活随笔為你收集整理的python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都大熊猫基地老人免票吗
- 下一篇: python 二项分布_二项分布的理论基