博客
关于我
Python菱形继承的初始化问题和继承顺序
阅读量:567 次
发布时间:2019-03-09

本文共 2478 字,大约阅读时间需要 8 分钟。

Python中的菱形继承解决了多层多继承中的初始化和查找方法问题。以下是优化后的内容:


菱形继承的初始化问题

在Python中,类通过多层继承和多继承形成复杂的继承结构时,子类的初始化方法可能被多次调用,导致父类的初始化代码重复执行。例如:

class Electrical(object):    def __init__(self, name):        self.name = name        print('Electrical init')class Phone(Electrical):    def __init__(self, name, price):        Electrical.__init__(self, name)        self.price = price        print('Phone init')class Computer(Electrical):    def __init__(self, name, config):        Electrical.__init__(self, name)        self.config = config        print('Computer init')class HuaWei(Phone, Computer):    def __init__(self, name, price, config):        Phone.__init__(self, name, price)        Computer.__init__(self, name, config)        print('HuaWei init')h = HuaWei('huawei', 100, 'i7')

运行结果显示,Electrical的初始化执行了两次,导致重复打印。这是因为HuaWei同时继承了Phone和Computer,两者都继承了Electrical。在HuaWei的初始化方法中,分别调用了Phone和Computer的初始化方法,而这两种覆盖了Electrical的初始化,导致重复执行。


解决初始化问题的方法

使用super()方法来解决重复初始化的问题。修改后的代码如下:

class Electrical(object):    def __init__(self, name):        self.name = name        print('Electrical init')class Phone(Electrical):    def __init__(self, price, *args):        super(Phone, self).__init__(*args)        self.price = price        print('Phone init')class Computer(Electrical):    def __init__(self, config, *args):        super(Computer, self).__init__(*args)        self.config = config        print('Computer init')class HuaWei(Phone, Computer):    def __init__(self, name, price, config):        super(HuaWei, self).__init__(name, price, config)        print('HuaWei init')h = HuaWei('huawei', 100, 'i7')

运行结果调整为:

Electrical initComputer initPhone initHuaWei init

这样确保了每个父类的初始化只执行一次,只有HuaWei的初始化方法覆盖了两个父类的初始化。


菱形继承的查找顺序

在菱形继承结构中,属性和方法的查找顺序按照广度优先算法进行:

  • 首先检查HuaWei类本身。
  • 如果找不到,检查其最近的父类(Phone)。
  • 如果仍找不到,继续检查第二个父类(Computer)。
  • 最后,如果不在HuaWei和其父类中找到,查找更远的祖先类( Electrical)。
  • 如果依然找不到,查找object类。
  • 例如:

    class HuaWei(Phone, Computer):    passh = HuaWei()h.game()  # 调用Phone的 game 方法h.watch_movie()  # 调用Computer的 watch_movie 方法h.chat()  # 调用Electrical的 chat 方法

    运行结果示:

    Play game in phone!Watch movie in computer!Chat with friend in electrical!

    这表明查找顺序是按照HuaWei → Phone → Computer → Electrical 的顺序进行。


    使用__mro__方法

    在Python中,可以使用__mro__方法查看类的方法解析顺序(MRO),它展示了按照何种顺序查找方法和属性。例如:

    print(HuaWei.__mro__)# 输出结果显示继承顺序:huawei → phone → computer → electrical → object

    这使用户能够清晰地了解类的MRO结构,指导方法和属性的查找过程。


    总结

    通过使用super()方法,确保了菱形继承结构中每个类的初始化只执行一次。同时,理解__mro__方法的作用,明确了方法和属性查找的顺序,这对于编写和维护复杂的继承结构至关重要。正确配置MRO顺序,不仅解决了初始化问题,还确保了属性和方法的检索逻辑清晰易懂。

    转载地址:http://myppz.baihongyu.com/

    你可能感兴趣的文章
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>
    Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
    查看>>
    poj 3277 线段树
    查看>>
    POJ 3349 Snowflake Snow Snowflakes
    查看>>
    POJ 3411 DFS
    查看>>
    poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
    查看>>
    Qt笔记——官方文档全局定义(二)Functions函数
    查看>>
    POJ 3468 A Simple Problem with Integers
    查看>>
    poj 3468 A Simple Problem with Integers 降维线段树
    查看>>
    poj 3468 A Simple Problem with Integers(线段树 插线问线)
    查看>>
    poj 3485 区间选点
    查看>>
    poj 3518 Prime Gap
    查看>>
    poj 3539 Elevator——同余类bfs
    查看>>