page116.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 94 行
HTML
94 行
<HTML><HEAD><TITLE>Abstract Objects</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="tex2html2553" HREF="page117.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2551" HREF="page115.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2545" HREF="page115.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2555" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION005221000000000000000">Abstract Objects</A></H3><P>Most of the classes described in this bookare derived from a common base class called <tt>Object</tt>.As shown in Figure <A HREF="page116.html#figclasses1"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the <tt>Object</tt> class is an <em>abstract</em> class that extendsthe <tt>__builtin__</tt><tt>.object</tt> class<P><P><A NAME="4399"> </A><A NAME="figclasses1"> </A> <IMG WIDTH=575 HEIGHT=361 ALIGN=BOTTOM ALT="figure4395" SRC="img644.gif" ><BR><STRONG>Figure:</STRONG> Object class hierarchy.<BR><P><P>Program <A HREF="page116.html#progobjecta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the Python codethat defines the <tt>Object</tt> class.<P><P><A NAME="4935"> </A><A NAME="progobjecta"> </A> <IMG WIDTH=575 HEIGHT=352 ALIGN=BOTTOM ALT="program4404" SRC="img645.gif" ><BR><STRONG>Program:</STRONG> <tt>Object</tt> class definition.<BR><P><P>Notice that the <tt>Object</tt> classdefines a method called <tt>__cmp__</tt>.This instance method takes a specified object and comparesit with the given object instance.The method returns an integer that is less than, equal to, or greater than zerodepending on whether this object instance isless than, equal to, or greater thanthe specified object instance <tt>obj</tt>,respectively.<P>The <tt>__cmp__</tt> method is special in several respects.First, it is the method that is called by the standard <tt>cmp</tt> function.I.e., given two <tt>Object</tt> instances,the following statements are equivalent:<PRE>result = cmp(obj1, obj2)result = obj1.__cmp__(obj2)</PRE>Second, the <tt>__cmp__</tt> method is automatically calledwhenever one of the comparison operators,(<tt><</tt>,<tt><=</tt>,<tt>==</tt>,<tt>!=</tt>,<tt>></tt>, and<tt>>=</tt>)is used to compare two <tt>Object</tt> instances.For example,the following statements are equivalent:<PRE>result = obj1 < obj2result = obj1.__cmp__(obj2) < 0</PRE><P>Notice how the <tt>__cmp__</tt> method of the <tt>Object</tt> class is implemented.When comparing two object instances, <tt>obj1</tt> and <tt>obj1</tt>,the comparison works as follows:<OL><LI> If the class of <tt>obj1</tt> is a subclass of the class of <tt>obj2</tt>, then the result of the comparison is <tt>obj1._compareTo(obj2)</tt>.<LI> If the class of <tt>obj2</tt> is a subclass of the class of <tt>obj1</tt>, then the result of the comparison is <tt>obj2._compareTo(obj1)</tt>.<LI> If the classes are unrelated, the the result is obtained by comparing the <em>names</em> of the classes.</OL>The <tt>_compareTo</tt> method is declared to be an <em>abstract</em> method.This means that it one must be provided in the implementationof every concrete class derived from the <tt>Object</tt> base class.The implementation of the <tt>_compareTo</tt> method for a given classis simplified because it will always be the case that the givenclass is a subclass of the class of the <tt>_compareTo</tt> argument.<P>The use of polymorphism in the wayshown gives the programmer enormous leverage.The fact most objects are derived from the <tt>Object</tt> base class,together with the fact that every concrete classmust implement an appropriate <tt>_compareTo</tt> method,ensures that the comparison operators can be used to compare anypair of <tt>Object</tt>sand that the comparisons always work as expected.<P><HR><A NAME="tex2html2553" HREF="page117.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2551" HREF="page115.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2545" HREF="page115.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2555" 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 + -
显示快捷键?