1. 若是子類定義了本身的__init__構造方法函數,當子類的實例對象被建立時,子類只會執行本身的__init__方法函數,若是子類未定義本身的構造方法函數,會沿着搜索樹找到父類的構造方法函數去執行父類裏的構造方法函數。python
2. 如子類定義了本身的構造方法函數,若是子類的構造方法函數內沒有主動調用父類的構造方法函數,那父類的實例變量在子類不會在剛剛建立子類實例對象時出現了。ide
[root@leco day8]# cat t4.py 函數
#!/usr/bin/env python class aa: def __init__(self): self.x = 10 self.y = 12 def hello(self, x): return x + 1 class bb(aa): def __init__(self): aa.__init__(self) self.z = 14 a = aa() print a.x, a.y b = bb() print b.x, b.y
[root@leco day8]# python t4.py 對象
10 12it
10 12ast
要是沒有調用父類的構造函數結果報錯class
[root@leco day8]# cat t4.py 變量
#!/usr/bin/env python class aa: def __init__(self): self.x = 10 self.y = 12 def hello(self, x): return x + 1 class bb(aa): def __init__(self): #aa.__init__(self) self.z = 14 a = aa() print a.x, a.y b = bb() print b.x, b.y
[root@leco day8]# python t4.py module
10 12搜索
Traceback (most recent call last):
File "t4.py", line 18, in <module>
print b.x, b.y
AttributeError: bb instance has no attribute 'x'