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'