本文实例讲述了Python面向对象程序设计多继承和多态用法。分享给大家供大家参考,具体如下:
多继承
就是一个子类继承多个父类:
多继承的例子,如下:
# -*- coding:utf-8 -*-
#! python3
class Base(object):
def test(self):
print("------base")
class A(Base):
def test1(self):
print("-----test1")
class B(Base):
def test2(self):
print("----test2")
class C(A,B):
pass
c=C()
c.test1()
c.test2()
c.test()
运行结果:
-----test1
----test2
------base
C也能继承Base
注:多继承中,每个父类都有相同的方法,子类继承时,会有一个继承顺序
想要查看该顺序的调用流程可以使用以下方法:
最后调用的是object方法,如果object方法也不存在,说明类中没有这个方法
print(子类类名.__mro__)