• super 小记


    Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

    Python3.x 实例:

    class A:
    pass
    class B(A):
    def add(self, x):
    super().add(x)
    Python2.x 实例:

    class A(object): # Python2.x 记得继承 object
    pass
    class B(A):
    def add(self, x):
    super(B, self).add(x)
    返回值
    无。

    实例
    以下展示了使用 super 函数的实例:

    !/usr/bin/python

    -- coding: UTF-8 --

    class FooParent(object):
    def init(self):
    self.parent = 'I'm the parent.'
    print ('Parent')

    def bar(self,message):
        print ("%s from Parent" % message)
    

    class FooChild(FooParent):
    def init(self):
    # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
    super(FooChild,self).init()
    print ('Child')

    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
    

    if name == 'main':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')
    执行结果:

    Parent
    Child
    HelloWorld from Parent
    Child bar fuction
    I'm the parent.

  • 相关阅读:
    python数据1-5
    python密码1-2
    css Loading 教程
    定制化jQuery
    PHP 将MySQL数据导出csv
    windows MySQL 5.7 导出表方法记录
    前端UI
    我的框架说明文档 2016-04-06
    微信公众号内支付(三)
    微信公众号内支付(二)
  • 原文地址:https://www.cnblogs.com/wdmx/p/9883874.html
Copyright © 2020-2023  润新知