脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 在python里面运用多继承方法详解

在python里面运用多继承方法详解

2021-07-28 00:21燃烧的猛男1 Python

在本篇文章中小编给各位分享的是关于在python里面运用多继承方法以及知识点总结,有兴趣的朋友们可以学习下。

如何在PYTHON里面运用多继承

  1. class Father:
  2.  
  3. def hobby(self):
  4.  
  5. print("love to play video game.")
  6.  
  7. class Mother:
  8.  
  9. def cook(self):
  10.  
  11. print("love to cook anything.")

#比如说有两个类,如果想要一个子类同时继承这两个类,应该怎么操作呢。

在python里面运用多继承方法详解

  1. class Father:
  2.  
  3. def hobby(self):
  4.  
  5. print("love to play video game.")
  6.  
  7. class Mother:
  8.  
  9. def cook(self):
  10.  
  11. print("love to cook anything.")
  12.  
  13. class Son(Father, Mother):
  14.  
  15. pass
  16.  
  17. son = Son()
  18.  
  19. son.hobby()
  20.  
  21. son.cook()

#只要在子类名称的后面加入两个父类,就可以进行多继承了。

在python里面运用多继承方法详解

  1. class Father:
  2.  
  3. def hobby(self):
  4.  
  5. print("love to play video game.")
  6.  
  7. def cook(self):
  8.  
  9. print("love to cook anything.")
  10.  
  11. class Mother:
  12.  
  13. def cook(self):
  14.  
  15. print("love to cook anything.")
  16.  
  17. def hobby(self):
  18.  
  19. print("love to play video game.")
  20.  
  21. class Son(Father, Mother):
  22.  
  23. pass
  24.  
  25. son = Son()
  26.  
  27. son.hobby()
  28.  
  29. son.cook()

#但是如果子类继承的时候,发现两个父类的方法都是一模一样的,那就没法同时继承两人的了。

在python里面运用多继承方法详解

  1. class Father:
  2.  
  3. def hobby(self):
  4.  
  5. print("love to play video game.")
  6.  
  7. def cook(self):
  8.  
  9. print("love to cook anything.")
  10.  
  11. class Mother:
  12.  
  13. def cook(self):
  14.  
  15. print("love to cook anything.")
  16.  
  17. def hobby(self):
  18.  
  19. print("love to play video game.")
  20.  
  21. class Son(Mother, Father):
  22.  
  23. pass
  24.  
  25. son = Son()
  26.  
  27. son.hobby()
  28.  
  29. son.cook()

#即使我们把位置调换了一下也是比较难看出究竟继承了谁。

在python里面运用多继承方法详解

  1. print(Son.__mro__)

#实际上我们可以用mro来查看顺序,首先是子类,然后是母亲,接着是父亲,这是根据书写顺序的,最后就是object了。

在python里面运用多继承方法详解

  1. class AAA(object):
  2.  
  3. pass
  4.  
  5. aaa = AAA()
  6.  
  7. dir(aaa)

#我们可以查看object类里面有什么方法。

在python里面运用多继承方法详解

  1. class AAA:
  2.  
  3. pass
  4.  
  5. aaa = AAA()
  6.  
  7. dir(aaa)

#如果没有基类,一般还是要在后面加上object,这里PYTHON3,所以有加和没加没有区别,但还是建议要加上。

在python里面运用多继承方法详解

Python多继承实例扩展:

多继承的使用

  1. #1.多继承:子类有多个父类
  2.  
  3. class Human:
  4. def __init__(self, sex):
  5. self.sex = sex
  6.  
  7. def p(self):
  8. print("这是Human的方法")
  9.  
  10. class Person:
  11. def __init__(self, name):
  12. self.name = name
  13.  
  14. def p(self):
  15. print("这是Person的方法")
  16.  
  17. def person(self):
  18. print("这是我person特有的方法")
  19.  
  20. class Teacher(Person):
  21. def __init__(self, name, age):
  22. super().__init__(name)
  23. self.age = age
  24.  
  25. class Student(Human, Person):
  26. def __init__(self, name, sex, grade):
  27. #super().__init__(name) #注意:对于多继承来说,使用super只会调用第一个父类的属性方法
  28. #super().__init__(sex) #要想调用特定父类的构造器只能使用父类名.__init__方式。如下:
  29.  
  30. Human.__init__(self,sex)
  31. Person.__init__(self,name)
  32. self.grade = grade
  33.  
  34. class Son(Human, Teacher):
  35. def __init__(self, sex, name, age, fan):
  36. Human.__init__(self, sex)
  37. Teacher.__init__(self, name, age)
  38. self.fan = fan
  39.  
  40. # ------创建对象 -------------
  41. stu = Student("tom", "male", 88)
  42. print(stu.name,stu.sex,stu.grade)
  43. stu.p() # 虽然父类Human和Person都有同名P()方法 ,但是调用的是括号里的第一个父类Human的方法
  44.  
  45. son1 = Son("jerry", "female", 18, "打球")
  46. son1.person() # 可以调用父类的父类的方法。
  47. son1.p() # 子类调用众多父类中同名的方法,按继承的顺序查找。
  48. =====================================================================================
  49. tom male 88
  50. 这是Human的方法
  51. 这是我person特有的方法
  52. 这是Human的方法

以上就是关于Python里多继承的知识点总结,感谢大家的阅读和对我们的支持。

延伸 · 阅读

精彩推荐