浅谈Python面向对象中的继承与mro

继承

python中的继承的含义就是其字面意思, 子类继承父类中的属性和方法, 且可以同时继承多个父类, 子类下面还可以再继续划分子类的子类。形成一种树形的结构, 最顶部的类叫做基类, 基类下面的子类等被称为派生类

在子类中可以定于跟父类中名称相同的方法和属性, 这些行为被称之为重写(重构), 在调用时优先调用当前类中的方法, 如果在当前类中搜索不到则树形结构向上递归查找。
PS: Python中所有的类都默认继承一个基类 Object

示例代码


class A: # 等价于 class A(Object):
    def __init__(self, name):
        self.name = name

    def run(self):
        return "running with class A"


class B:
    def __init__(self, name):
        self.name = name

    def run(self):
        return "running with class B"

class C(B, A):

    def space(self):
        pass

# a既是C的实例, 也是A,B的实例
a = C("takahashi")

# 按mro结构查找方法
print(a.run())

>>> running with class B

Mro


Python的MRO即Method Resolution Order(方法解析顺序),即在调用方法时,会对当前类以及所有的基类进行一个搜索,以确定该方法之所在,而这个搜索的顺序就是MRO。

Mro搜索原则:

  1. 子类永远在父类前面
    2. 如果有多个父类,则按mro列表中的顺序被调用
    3. 如果下一个类中存在两个合法的选择, 则只选择第一个

如果需要查看类的mro调用顺序只需要打印ClassName.mro()即可, 返回一个列表。

示例代码


class A:

    def __init__(self):
        print("Class A now is start!")

class B(A):

    def __init__(self):
        super().__init__() # 此处先调用了class A中的__init__方法
        print("Class B now is start!")

class C:

    def __init__(self):
        print("Class C now is start!")


class inherit(A, C):
    def display(self):
        print(inherit.mro())


class other_inherit(B, C):
    def display(self):
        print(other_inherit.mro())


class another_inherit(C, B):
    def display(self):
        print(another_inherit.mro())

class last_inherit(other_inherit, C):
    def display(self):
        print(last_inherit.mro())



print("----------------------cut-line-----------------------")
a = inherit()
a.display()
print("----------------------cut-line-----------------------")
b = other_inherit()
b.display()
print("----------------------cut-line-----------------------")
c = another_inherit()
c.display()
print("----------------------cut-line-----------------------")
d = last_inherit()
d.display()
print("----------------------cut-line-----------------------")
>>> ----------------------cut-line-----------------------
>>> Class A now is start!
>>> [<class '__main__.inherit'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
>>> ----------------------cut-line-----------------------
>>> Class A now is start!
>>> Class B now is start!
>>> [<class '__main__.other_inherit'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
>>> ----------------------cut-line-----------------------
>>> Class C now is start!
>>> [<class '__main__.another_inherit'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
>>> ----------------------cut-line-----------------------
>>> Class A now is start!
>>> Class B now is start!
>>> [<class '__main__.last_inherit'>, <class '__main__.other_inherit'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
>>> ----------------------cut-line-----------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值