page600.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 104 行
HTML
104 行
<HTML><HEAD><TITLE>Derivation and 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="tex2html8046" HREF="page601.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8044" HREF="page599.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8038" HREF="page599.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8048" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0017610000000000000000">Derivation and Inheritance</A></H2><P>This section reviews the concept of a derived class.Derived classes are an extremely useful feature of Python becausethey allow the programmer to define new classes by extending existing classes.By using derived classes,the programmer can exploit the commonalitiesthat exist among the classes in a program.Different classes can share values and operations.<P><em>Derivation</em><A NAME=57147> </A> is the definition of a new classby extending an existing class.The new class is called the <em>derived class</em><A NAME=57149> </A>and the existing class from which it is derivedis called the <em>base class</em><A NAME=57151> </A>.<P>In Python there must be at least one base class,but there may be more than one base class(<em>multiple inheritance</em><A NAME=57153> </A><A NAME=57154> </A>).<P>Python supports so-called<em>classic classes</em><A NAME=57156> </A><A NAME=57157> </A>and<em>new-style classes</em><A NAME=57159> </A><A NAME=57160> </A>.A new-style class is one that is ultimately derived fromthe built-in <tt>object</tt> class.A classic class is one that does not have a base classor one that is derived only from other classic classes.Classic classes are being phased out of the Python langaugeand are not used in this book.<P>Consider the <tt>Person</tt> class defined in Program <A HREF="page600.html#progpersona"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>and the <tt>Parent</tt> class defined in Program <A HREF="page600.html#progparenta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Because parents are people too,the <tt>Parent</tt> class is derived from the <tt>Person</tt> class.Derivation in Python is indicated by includingthe name(s) of the base class(es) in parenthesesin the declaration of the derived class.<P><P><A NAME="57517"> </A><A NAME="progpersona"> </A> <IMG WIDTH=575 HEIGHT=239 ALIGN=BOTTOM ALT="program57168" SRC="img2490.gif" ><BR><STRONG>Program:</STRONG> <tt>Person</tt> class.<BR><P><P><P><A NAME="57521"> </A><A NAME="progparenta"> </A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program57179" SRC="img2491.gif" ><BR><STRONG>Program:</STRONG> <tt>Parent</tt> class.<BR><P><P>A derived class <em>inherits</em><A NAME=57193> </A>all the attributes of its base class.That is, the derived class contains all the class attributes containedin the base class and the derived class supports all the same operationsprovided by the base class.For example, consider the following statements:<PRE>p = Person()q = Parent()</PRE>Since <tt>p</tt> is a <tt>Person</tt>,it has the instance attributes <tt>_name</tt> and <tt>_sex</tt>and method <tt>__str__</tt>.Furthermore,since <tt>Parent</tt> is derived from <tt>Person</tt>,then the object <tt>q</tt> also hasthe instance attributes <tt>_name</tt> and <tt>_sex</tt>and method <tt>__str__</tt>.<P>A derived class can <em>extend</em> the base class in several ways:New instance attributes can be used,new methods can be defined,and existing methods can be <em>overridden</em><A NAME=57207> </A>.For example, the <tt>Parent</tt> class adds the instance attribute<tt>_children</tt> and the method <tt>getChild</tt>.<P>If a method is defined in a derived classthat has the same name as a method in a base class,the method in the derived class <em>overrides</em><A NAME=57212> </A>the one in the base class.For example,the <tt>__str__</tt> method in the <tt>Parent</tt> classoverrides the <tt>__str__</tt> method in the <tt>Person</tt> class.Therefore, <tt>str(p)</tt> invokes <tt>Person.__str__</tt>,whereas <tt>str(q)</tt> invokes <tt>Parent.__str__</tt>.<P>An instance of a derived class can be used anywhere in a programwhere an instance of the base class may be used.For example, this means that a <tt>Parent</tt>may be passed as an actual parameter to a methodthat expects to receive a <tt>Person</tt>.<P><HR><A NAME="tex2html8046" HREF="page601.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8044" HREF="page599.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8038" HREF="page599.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8048" 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 + =
减小字号Ctrl + -
显示快捷键?