📄 page423.html
字号:
<HTML><HEAD><TITLE>When Objects Refer to Other 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="tex2html6051" HREF="page424.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6049" HREF="page422.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6043" HREF="page422.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6053" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0013210000000000000000">When Objects Refer to Other Objects</A></H2><P>The <tt>int</tt> objects considered in the preceding examplesare very simple objects--they contain no references to other objects.Reference counting is an ideal strategy for garbage collecting such objects.But what about objects that refer to other objects?For example, consider the <tt>Association</tt> class described in Chapter <A HREF="page111.html#chapadts"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>which represents a <IMG WIDTH=76 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67437" SRC="img1692.gif" > pair.We can still use reference counting,provided we count all references to an object<em>including references from other objects</em>.<P>Figure <A HREF="page423.html#figgarbage3"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (a) illustrates the contents memory followingthe execution of this statement:<PRE>p = Association(int(57), int(99))</PRE>The reference count of the <tt>Association</tt> is one,because the variable <tt>p</tt> refers to it.Similarly, the reference counts of the two <tt>int</tt> instances are onebecause the <tt>Association</tt> refers to both of them.<P><P><A NAME="30373"> </A><A NAME="figgarbage3"> </A> <IMG WIDTH=575 HEIGHT=422 ALIGN=BOTTOM ALT="figure30098" SRC="img1693.gif" ><BR><STRONG>Figure:</STRONG> Reference counting when objects refer to other objects.<BR><P><P>Suppose we assign the value <tt>None</tt> to the variable <tt>p</tt>.As shown in Figure <A HREF="page423.html#figgarbage3"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (b),the reference count of the association becomes zero--it is now garbage.However, until the <tt>Association</tt> instance continues to exist untilit is garbage collected.And because it still exists,it still refers to the <tt>int</tt> objects.<P>Figure <A HREF="page423.html#figgarbage3"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (d) shows that the garbage collection processadjusts the reference counts on the objects to which the association refersonly when the association is garbage collected.The two <tt>int</tt> objects are now unreferencedand can be garbage collected as well.<P><HR><A NAME="tex2html6051" HREF="page424.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6049" HREF="page422.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6043" HREF="page422.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6053" 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 + -