• 技术文章 >常见问题 >Python常见问题

    python支持多继承吗

    silencementsilencement2019-11-16 09:22:19原创1987

    多继承

    单继承有时候可能满足不了我们所需的所以我们就会遇到多继承,这个同样能够展示出代码的重用。

    同样是上边的例子,child不仅仅是继承来自父亲,还继承来自母亲。所以我们创建mother类

    class Mother(object):
        def __init__(self,face):
            self.face=face
            print('face',face)
        def play(self):
            print('mother go shopping with me')

    mothe类创建的属性为face,其次我们还定义的一个相同的方法play 是为了展示多继承中如果有相同的函数会调用哪个。

    然后我们重写一下child类

    from Father import Father
    from Mother import Mother
    class Child(Mother,Father):
        def __init__(self,money,face):
            Father.__init__(self, money)
            Mother.__init__(self,face)

    python学习网,免费的python学习网站,欢迎在线学习!

    专题推荐:继承
    上一篇:python assert是什么 下一篇:python怎样逐行读取

    相关文章推荐

    • python继承是如何实现的• Python类的继承机制是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网