Python面向对象—类的继承
2018-08-05 22:10
471 查看
一个子类可以继承父类的所有属性,不管是父类的数据属性还是方法。
class Father(object): num = 0 def __init__(self): print "I'm Parent init" def foo(self): print "I'm Parent foo" class Children(Father): def __init__(self): super(Children, self).__init__() print "I'm Child init" def foo(self): Father.foo(self) # 访问父类被覆盖的方法 print "I'm Child foo" ob = Children() print ob.num #访问父类属性 ob.foo() Father.foo(ob) # 访问父类被覆盖的方法
结果输出为
I'm Parent init I'm Child init 0 I'm Parent foo I'm Child foo I'm Parent foo
多继承
经典类:深度优先,从左至右
新式类:广度优先,从左至右
新式类与经典类的区别在于是否有继承object基类
class P1: #(object): def foo(self): print "I'm P1-foo" class P2: #(object): def foo(self): print "I'm P2-foo" def bar(self): print "I'm P2-bar" class C1(P1, P2): pass class C2(P1, P2): def bar(self): print "I'm C2-bar" class GC(C1, C2): pass gc = GC() gc.foo() gc.bar()
它们的关系图如下
当(object)注释掉,变为经典类时,会先查找最亲的父类,结果为
I'm P1-foo # GC -> C1 -> P1 I'm P2-bar # GC -> C1 -> P1 -> P2,如果P2没有bar()方法,则返回从C2搜索
当(object)取消注释,变为新式类时,会先查找同胞类,结果为
I'm P1-foo # GC -> C1 -> C2 -> P1 I'm C2-bar # GC -> C1 -> C2 #找到相应方法,就不会继续继续查找父类
新式类中可以使用__mro__属性来显示查找顺序
print GC.__mro__ #结果为 #(<class '__main__.GC'>, <class '__main__.C1'>, <class '__main__.C2'>, <class '__main__.P1'>, <class '__main__.P2'>, <type 'object'>)
相关文章推荐
- 初学Python的学习笔记8----面向对象、数据封装、访问限制、继承和多态
- python的面向对象的特性(继承、封装、多态)
- python面向对象(三)之继承
- Python基础(面向对象之封装与继承)
- python之面向对象之继承
- python 面向对象——继承与多态
- python 面向对象-继承
- python面向对象三大特性之继承、多态、封装
- Python第九天 面向对象 类定义 类的属性 类的方法 内部类 垃圾回收机制 类的继承 装饰器
- python面向对象--封装,继承,多态
- python 面向对象三大特性(封装 多态 继承)
- python 面向对象 继承
- python面向对象(三)之继承
- Python3 与 C# 面向对象之~继承与多态
- python语法学习面向对象之继承
- Python面向对象之继承代码详解
- Python面向对象之继承和组合用法实例分析
- python之面向对象(继承)
- python 面向对象(三)多继承