page127.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 77 行

HTML
77
字号
<HTML><HEAD><TITLE>Associations</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="tex2html2669" HREF="page128.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2667" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2663" HREF="page126.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2671" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION005270000000000000000">Associations</A></H2><A NAME="secadtsassociations">&#160;</A><P>An association is an ordered pair of objects.The first element of the pair is called the <em>key</em><A NAME=4801>&#160;</A>;the second element is the <em>value</em><A NAME=4803>&#160;</A>associated with the given key.<P>Associations are useful for storinginformation in a database<A NAME=4804>&#160;</A> for later retrieval.For example, a database can be viewed as a container that holdskey-and-value pairs.The information associated with a given keyis retrieved from the database by searching the databasefor an the ordered pair in which the key matches the given key.<P>Program&nbsp;<A HREF="page127.html#progassociationa"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introduces the <tt>Association</tt> class.The <tt>Association</tt> class concrete extensionof the abstract <tt>Object</tt> class given in Program&nbsp;<A HREF="page116.html#progobjecta"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="4988">&#160;</A><A NAME="progassociationa">&#160;</A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program4810" SRC="img654.gif"  ><BR><STRONG>Program:</STRONG> <tt>Association</tt> class <tt>__init__</tt> method.<BR><P><P>As shown in Program&nbsp;<A HREF="page127.html#progassociationa"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,we implement an association using a Python tuple<A NAME=4818>&#160;</A>.The <tt>__init__</tt> method always creates a tuples with exactly two elements.The first element of the tuple represents the key of the associationand the second element of the tuple represents the valueassociated with the given key.<P>Two properties of an <tt>Association</tt> are defined in Program&nbsp;<A HREF="page127.html#progassociationb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>key</tt> property returns the value of the first element of the tupleand the <tt>value</tt> property returnsthe value of the second element of the tuple.<P><P><A NAME="4989">&#160;</A><A NAME="progassociationb">&#160;</A> <IMG WIDTH=575 HEIGHT=294 ALIGN=BOTTOM ALT="program4824" SRC="img655.gif"  ><BR><STRONG>Program:</STRONG> <tt>Association</tt> properties.<BR><P><P>The remaining methods of the <tt>Association</tt> classare defined in Program&nbsp;<A HREF="page127.html#progassociationc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>_compareTo</tt> method is used to compare associations.Its argument is an object that is assumedto be an instance of the <tt>Association</tt> class.The <tt>_compareTo</tt> method is one place wherean association distinguishes between the key and the value.In this case, the result of the comparison is based solelyon the keys of the two associations--the values have no role in the comparison.<P><P><A NAME="4994">&#160;</A><A NAME="progassociationc">&#160;</A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program4843" SRC="img656.gif"  ><BR><STRONG>Program:</STRONG> <tt>Association</tt> methods.<BR><P><P>Program&nbsp;<A HREF="page127.html#progassociationc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> also defines a <tt>__str__</tt> method.The purpose of the <tt>__str__</tt> method is to return a textualrepresentation of the association.In this case,the implementation is trivial and needs no further explanation.<P><HR><A NAME="tex2html2669" HREF="page128.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2667" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2663" HREF="page126.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2671" 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 &#169; 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 + -
显示快捷键?