zoukankan      html  css  js  c++  java
  • Python面向对象—类的继承

    一个子类可以继承父类的所有属性,不管是父类的数据属性还是方法。

    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'>)
  • 相关阅读:
    webstorm
    web大文件上传(web应用---SSH框架)
    Webuploader 大文件分片上传
    百度Webuploader 大文件分片上传(.net接收)
    java使用WebUploader做大文件的分块和断点续传
    大文件上传插件webupload插件
    使用Webuploader大文件分片传输
    使用原生Java Web来实现大文件的上传
    Java实现浏览器端大文件分片上传
    怎样使用word2013发布csdn博客
  • 原文地址:https://www.cnblogs.com/delav/p/9427551.html
Copyright © 2011-2022 走看看