📄 page606.html
字号:
<HTML><HEAD><TITLE>Multiple Inheritance</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html8112" HREF="page607.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8110" HREF="page599.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8106" HREF="page605.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8114" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0017630000000000000000">Multiple Inheritance</A></H2><P>In Python a class can be derived from one or more base classes.For example,consider the following class definitions:<PRE>class A(object): def f(): passclass B(object): pass def f(): passclass C(A, B): pass</PRE>The class <tt>C</tt> extends both classes <tt>A</tt> and <tt>B</tt>.Therefore,the <tt>C</tt> class inherits class attributes from both base classes.<P>An interesting question arises when more than one base classdefines an attribute with the same name.For example,<tt>A</tt> and <tt>B</tt> both define a method named <tt>f</tt>.Given an instance <tt>c</tt> of class <tt>C</tt>,what method does the expression <tt>c.f()</tt> call?<P>The method called is determined by a set of rules called the Python <em>method resolution order</em><A NAME=57439> </A>.In order to handle the general case,the rules are quite complex and are beyond the scope of this book.(For a thorough explanation, see [<A HREF="page610.html#simionato">44</A>]).However,a simple cases such as this,the rules are straightfoward:To find a name,first search the namespace of class <tt>C</tt>,and then search base classes in the order given.That is, search <tt>A</tt> first and then search <tt>B</tt>.Therefore, in this case the function invoked by the expression <tt>c.f()</tt>is the function <tt>A.f</tt>.<P><HR><A NAME="tex2html8112" HREF="page607.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8110" HREF="page599.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8106" HREF="page605.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8114" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -