page126.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 86 行
HTML
86 行
<HTML><HEAD><TITLE>Searchable Containers</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="tex2html2660" HREF="page127.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2658" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2652" HREF="page125.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2662" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION005260000000000000000">Searchable Containers</A></H2><A NAME="secadtssrchcont"> </A><P>A <em>searchable container</em> is an extension of the container abstraction.It adds to the set of methods provided for containersmethods for putting objects in and taking objects out,for testing whether a given object is in the container,and a method to search the container for a given object.<P>The definition of the <tt>SearchableContainer</tt> abstract class isshown in Program <A HREF="page126.html#progsearchableContainera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>SearchableContainer</tt> abstract class extends the <tt>Container</tt>absract class given in Program <A HREF="page119.html#progcontainera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It adds four new abstract methods(and overrides the <tt>__init__</tt> method).<P><P><A NAME="4981"> </A><A NAME="progsearchableContainera"> </A> <IMG WIDTH=575 HEIGHT=391 ALIGN=BOTTOM ALT="program4758" SRC="img653.gif" ><BR><STRONG>Program:</STRONG> <tt>SearchableContainer</tt> abstract class.<BR><P><P>The <tt>__contains__</tt> method is a <tt>bool</tt>-valuedmethod which takes as its argumentany object derived from the <tt>Object</tt> abstract base class.The purpose of this method is to test whether thegiven object instance is in the container.<P>The <tt>__contains__</tt> method is treated specially in Python.It is automatically called whenver the operator <tt>in</tt> is called.Thus,the following two statements are equivalent:<PRE>result = c.__contains__(obj)result = c in obj</PRE><P>The purpose of the <tt>insert</tt> methodis to put an object into the container.The <tt>insert</tt> method takes an <tt>Object</tt>and inserts it into the container.Similarly, the <tt>withdraw</tt> method is usedto remove an object from a container.The argument refers to the object to be removed.<P>The final method, <tt>find</tt>,is used to locate an object in a containerand to return a reference to that object.In this case,it is understood that the search is to be doneusing the comparison methods defined in the <tt>Object</tt> abstract base class.That is, the <tt>find</tt> method is <em>not</em> to be implementedas a search of the container for the given object instancebut rather as a search of the container for an objectthat compares equal to the given object.<P>This is an important subtlety in the semantics of <tt>find</tt>:The search is not for the given object,but rather for an object that compares equal to the given object.These semantics are particularly useful when using<em>associations</em><A NAME=4790> </A>,which are defined in Section <A HREF="page127.html#secadtsassociations"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P>In the event that the <tt>find</tt> method fails to findan object equal to the specified object,then it will return the object <tt>None</tt>.Therefore, the user of the <tt>find</tt> method should test explicitlythe returned value to determine whether the search was successful.Also, the <tt>find</tt> method does <em>not</em> remove the object it findsfrom the container.An explicit call of the <tt>withdraw</tt> method is neededto actually remove the object from the container.<P><HR><A NAME="tex2html2660" HREF="page127.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2658" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2652" HREF="page125.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2662" 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 + -
显示快捷键?