美文网首页
python 类的继承及super的研究

python 类的继承及super的研究

作者: YYL07 | 来源:发表于2018-02-07 15:07 被阅读0次

参考来源:
http://www.cnblogs.com/dkblog/archive/2011/02/24/1980654.html

类的继承

# -*- coding: utf-8 -*-


class BaseCla(object):
    def __init__(self):
        print 'I\'am baseCla'

    def run(self, speed):
        print 'parent run {}'.format(speed)


class Son(BaseCla):
    def __init__(self):
        super(Son, self).__init__()
        print 'I\'am expCla'

    def run(self, speed):
        print 'son run {}'.format(speed)
        super(Son, self).run(speed)


if __name__ == '__main__':
    s = Son()
    s.run(5)

说明:

  1. super(Son, self).run(speed)在python2.x中的写法。在python3.x中使用super().run(speed)即可,super不用传入参数。
  2. 父类的初始化化方法'init()'并不会自动调用,所以在子类中需要自己使用super调用。

分析: super的好处?为何要使用super,而不是直接用类名代替?

写法一:

# -*- coding: utf-8 -*-


class BaseCla(object):
    def __init__(self):
        print 'I\'am BaseCla'


class Son1(BaseCla):
    def __init__(self):
        BaseCla.__init__(self)
        print 'I\'am son1'


class Son2(BaseCla):
    def __init__(self):
        BaseCla.__init__(self)
        print 'I\'am son2'


class Grandson(Son1, Son2):
    def __init__(self):
        Son1.__init__(self)
        Son2.__init__(self)
        print 'I\'am Grandson'

if __name__ == '__main__':
    Grandson()

结果:
I'am BaseCla
I'am son1
I'am BaseCla
I'am son2
I'am Grandson

写法二:

# -*- coding: utf-8 -*-


class BaseCla(object):
    def __init__(self):
        print 'I\'am BaseCla'


class Son1(BaseCla):
    def __init__(self):
        super(Son1, self).__init__()
        print 'I\'am son1'


class Son2(BaseCla):
    def __init__(self):
        super(Son2, self).__init__()
        print 'I\'am son2'


class Grandson(Son1, Son2):
    def __init__(self):
        super(Grandson, self).__init__()
        print 'I\'am Grandson'

if __name__ == '__main__':
    Grandson()
    print Grandson.__mro__


结果:
I'am BaseCla
I'am son2
I'am son1
I'am Grandson
(<class '__main__.Grandson'>, <class '__main__.Son1'>, <class '__main__.Son2'>, <class '__main__.BaseCla'>, <type 'object'>)

分析:

  1. 使用super,避免直接使用类名,以后如果类名修改,不用修改代码。如果基类被多个子类继承,一旦改动,会造成多处代码修改。
  2. 如上两种方法继承,会发现第二种方法BaseCla只调用了一次,而第一种方法却调用的两遍。
  3. 第二种方法可以看出,super 的继承顺序是 son2 、son1。在super机制里可以保证公共父类仅被执行一次,至于执行的顺序,是按照mro进行的(Grandson.__mro__)。

相关文章

  • python 类的继承及super的研究

    参考来源:http://www.cnblogs.com/dkblog/archive/2011/02/24/198...

  • ubuntu 下基于python深度学习

    一.python 的基本语法 <1>Python 类的定义、继承及使用对象 注意super()的使用,成员函...

  • Python的继承

    #Python继承的特点 总是从某个类继承 不要忘记调用 super().init 一定要用 super(Teac...

  • python继承机制和Minxin模式

    python继承和访问父类和super python通过在类方法名旁边声明(父类名)来继承父类子类可以通过父类名....

  • python类继承super

    在学习python类继承的时候,有没有被super迷惑。原来这只是新式类的标志而已,并且可以改写所有上面父类的相同属性

  • python面向对象之继承

    1.python中继承一个类继承的形式是:class 子类(父类)一般的类我们让它继承自object。 super...

  • python类继承(super多类继承)

    1. python2和python3中定义类的形式不同 python3中只有只有新式类 python2中有经典类和...

  • 怎么理解Python类中的super函数

    前言 在Python类的继承中,经常能看到super函数的存在,那super函数主要的作用,以及如何理解和使用好这...

  • Python 类

    单纯的类 继承 super()函数帮助Python将父类跟子类联系起来, 父类也称为超类superclass在 P...

  • python 神奇的super()

    我们都知道在python继承中,子类调用父类的方法是使用super()函数,在2.X里格式是super(mycla...

网友评论

      本文标题:python 类的继承及super的研究

      本文链接:https://www.haomeiwen.com/subject/xdptfxtx.html