博客
关于我
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/

    你可能感兴趣的文章
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>