• Python中的Class


    父、子类的执行顺序

    当子类继承父类,子类和父类中有相同的函数,那么先执行哪个呢?

    1. class Animal():
    2. def run(self):
    3. print("动物会跑!")
    4. class People(Animal):
    5. def run(self):
    6. print("人会跑!")
    7. running = People()
    8. running.run()
    9. """
    10. Output:
    11. 人会跑!
    12. """

    当有__init__()函数时呢?

    1. #----------------父类中有__init__()------------------#
    2. class Animal():
    3. def __init__(self):
    4. print("动物开始准备...")
    5. def run(self):
    6. print("动物会跑!")
    7. class People(Animal):
    8. def run(self):
    9. print("人会跑!")
    10. running = People()
    11. running.run()
    12. """
    13. Output:
    14. 动物开始准备...
    15. 人会跑!
    16. """
    17. #----------------父子类都有__init__()------------------#
    18. class Animal():
    19. def __init__(self):
    20. print("动物开始准备...")
    21. def run(self):
    22. print("动物会跑!")
    23. class People(Animal):
    24. def __init__(self):
    25. print("人开始准备...")
    26. def run(self):
    27. print("人会跑!")
    28. running = People()
    29. running.run()
    30. """
    31. Output:
    32. 人开始准备...
    33. 人会跑!
    34. """

    可以看出,真是长江后浪推前浪,子类把父类淹没在沙滩上.....

    那么当子类继承了多个父类呢?这里举两个爸爸的栗子吧。

    1. class Animal1():
    2. def __init__(self):
    3. print("1开始准备...")
    4. def run(self):
    5. print("动物1会跑!")
    6. class Animal2():
    7. def __init__(self):
    8. print("2开始准备...")
    9. def run(self):
    10. print("动物2会跑!")
    11. class People(Animal1, Animal2):
    12. pass # 为了清楚展示父类调用顺序,子类就pass。
    13. # 当然,如果子类还有输出内容,还会优先输出子类内容
    14. """
    15. Output:
    16. 1开始准备...
    17. 动物1会跑!
    18. """
    19. #---------------看个有趣的,注意run函数命名--------------#
    20. class Animal1():
    21. def __init__(self):
    22. print("1开始准备...")
    23. def run(self):
    24. print("动物1会跑!")
    25. class Animal2():
    26. def __init__(self):
    27. print("2开始准备...")
    28. def run2(self):
    29. print("动物2会跑!")
    30. class People(Animal1, Animal2):
    31. pass
    32. """
    33. Output:
    34. 1开始准备...
    35. 动物2会跑!
    36. """

    可以看出,子类People会根据继承父类顺序来执行,还是跟最开始的爸爸最亲。就算调用了第②个爸爸的子函数,初始化的输出还是选择第①个爸爸。

    看一下内部结构,classname.mro():每一个继承父类的子类都有一个与方法解析顺序相关的特殊属性,__mro__装着方法解析时,对象的查找顺序,可以用这个属性查看类的执行顺序。可以看出,执行顺序是由子类先出发 → 第一个父类 → 第二个父类 →...→ 第n个父类 → object。

    mro通过元组方式存储顺序,可以通过访问其下标值访问类,以及类中的属性!

    1. People.mro()
    2. """
    3. Output:
    4. [__main__.People, __main__.Animal1, __main__.Animal2, object]
    5. """
    6. People.__mro__[2].run2(People)
    7. """
    8. Output:
    9. 动物2会跑!
    10. """
    11. # mro的使用方式
    12. # classname.mro()
    13. # classname.__mro__
    14. # classname().__class__.mro()

  • 相关阅读:
    [MRCTF2020]Ezpop-1|php序列化
    【Rust练习】18.特征 Trait
    【CSS】H9_CSS变形transform、过渡transition、CSS3动画 animation
    Shell 常用操作指令
    F - Thanos Sort
    HTTP1.1协议详解
    【Luogu P2221】[HAOI2012] 高速公路(线段树,期望)
    计算机软考高项(信息系统项目管理师)、中项(系统集成项目管理工程师),统计师中级的一些备考经验
    3、TCP状态
    大数据之LibrA数据库系统告警处理(ALM-12035 恢复任务失败后数据状态未知)
  • 原文地址:https://blog.csdn.net/weixin_42410915/article/details/125559900