44.md 3.8 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8
# Python 多重继承

> 原文: [https://www.programiz.com/python-programming/multiple-inheritance](https://www.programiz.com/python-programming/multiple-inheritance)

#### 在本教程中,您将学习 Python 中的多重继承以及如何在程序中使用它。 您还将了解多级继承和方法解析顺序。

## Python 多重继承

W
wizardforcel 已提交
9
[](/python-programming/class)可以从 Python 中的多个基类派生,类似于 C++ 。 这称为多重继承。
W
wizardforcel 已提交
10 11 12

在多重继承中,所有基类的功能都继承到派生类中。 多重继承的语法类似于单一[继承](/python-programming/inheritance)

W
wizardforcel 已提交
13
### 示例
W
wizardforcel 已提交
14 15 16 17 18 19 20 21 22 23 24 25

```py
class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass
```

W
wizardforcel 已提交
26
在此,`MultiDerived`类是从`Base1``Base2`类派生的。
W
wizardforcel 已提交
27

W
wizardforcel 已提交
28
![Multiple Inheritance in Python](img/768eee4ef17522c45f34ec1807376c4a.png "Multiple Inheritance")
W
wizardforcel 已提交
29

W
wizardforcel 已提交
30
Python 中的多重继承
W
wizardforcel 已提交
31 32


W
wizardforcel 已提交
33 34

`MultiDerived`类继承自`Base1``Base2`类。
W
wizardforcel 已提交
35 36 37

* * *

W
wizardforcel 已提交
38
## Python 多级继承
W
wizardforcel 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

我们还可以从派生类继承。 这称为多级继承。 在 Python 中可以是任何深度。

在多级继承中,基类和派生类的功能都被继承到新的派生类中。

下面给出了具有相应可视化效果的示例。

```py
class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass
```

W
wizardforcel 已提交
57 58 59
这里,`Derived1`类是从`Base`类派生的,`Derived2`类是从`Derived1`类派生的。

![Multilevel Inheritance in Python](img/5edd36180ec94e316d533bd69907f5ec.png "Multilevel Inheritance")
W
wizardforcel 已提交
60

W
wizardforcel 已提交
61
Python 中的多级继承
W
wizardforcel 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85



* * *

## Python 中的方法解析顺序

Python 中的每个类均源自`object`类。 它是 Python 中最基本的类型。

因此,从技术上讲,所有其他内置或用户定义的类都是派生类,并且所有对象都是`object`类的实例。

```py
# Output: True
print(issubclass(list,object))

# Output: True
print(isinstance(5.5,object))

# Output: True
print(isinstance("Hello",object))
```

在多继承方案中,将在当前类中首先搜索任何指定的属性。 如果未找到,则搜索将以深度优先,从左到右的方式继续进入父类,而无需两次搜索相同的类。

W
wizardforcel 已提交
86
因此,在上述`MultiDerived`类的示例中,搜索顺序为`MultiDerived``Base1``Base2``object`。 该顺序也称为`MultiDerived`类的线性化,用于查找该顺序的规则集称为**方法解析顺序(MRO)**
W
wizardforcel 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

MRO 必须防止本地优先级排序,并且还必须提供单调性。 它确保班级始终出现在其班级之前。 如果有多个父母,顺序与基类的元组相同。

类别的 MRO 可以视为`__mro__`属性或`mro()`方法。 前者返回一个元组,而后者返回一个列表。

```py
>>> MultiDerived.__mro__
(<class '__main__.MultiDerived'>,
 <class '__main__.Base1'>,
 <class '__main__.Base2'>,
 <class 'object'>)

>>> MultiDerived.mro()
[<class '__main__.MultiDerived'>,
 <class '__main__.Base1'>,
 <class '__main__.Base2'>,
 <class 'object'>]
```

这是一个稍微复杂的多重继承示例及其可视化以及 MRO。

W
wizardforcel 已提交
108 109
![Multiple Inheritance Visualization](img/db8bf444f77f36a15838f8e99e9ce24a.png "Multiple Inheritance Visualization")

W
wizardforcel 已提交
110
在 Python 中可视化多重继承
W
wizardforcel 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150



```py
# Demonstration of MRO

class X:
    pass

class Y:
    pass

class Z:
    pass

class A(X, Y):
    pass

class B(Y, Z):
    pass

class M(B, A, Z):
    pass

# Output:
# [<class '__main__.M'>, <class '__main__.B'>,
#  <class '__main__.A'>, <class '__main__.X'>,
#  <class '__main__.Y'>, <class '__main__.Z'>,
#  <class 'object'>]

print(M.mro())
```

**输出**

```py
[<class '__main__.M'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.Z'>, <class 'object'>]
```

要了解有关如何计算 MRO 的实际算法,请访问[关于 MRO 的讨论](http://www.python.org/download/releases/2.3/mro/)