⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page120.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
字号:
<HTML><HEAD><TITLE>Container Properties</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="tex2html2595" HREF="page121.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2593" HREF="page118.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2589" HREF="page119.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2597" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION005232000000000000000"><tt>Container</tt> Properties</A></H3><P>Program&nbsp;<A HREF="page120.html#progcontainerb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the methods<tt>getCount</tt>, <tt>getIsEmpty</tt> and <tt>getIsFull</tt>.The <tt>getCount</tt> method simply returnsthe number of objects in the container.<P><P><A NAME="4954">&#160;</A><A NAME="progcontainerb">&#160;</A> <IMG WIDTH=575 HEIGHT=409 ALIGN=BOTTOM ALT="program4540" SRC="img648.gif"  ><BR><STRONG>Program:</STRONG> <tt>Container</tt> methods and properties.<BR><P><P>The <tt>getIsEmpty</tt> and <tt>getIsFull</tt> methodsare <tt>bool</tt>-valued methodswhich indicate whether a givencontainer is empty or full, respectively.Notice that the <tt>getIsEmpty</tt> <tt>get</tt> accessordoes not directly access the <tt>count</tt> instance attribute.Instead it uses <tt>Count</tt> property.As long as the <tt>Count</tt> property has the correct semantics,the <tt>IsEmpty</tt> property will too.<P>In some cases, a container is implemented in a waywhich makes its capacity finite.When this is the case,it is necessary to be able to determine when the container is full.<tt>IsFull</tt> is a <tt>bool</tt>-valued propertythat provides a <tt>get</tt> accessorthat returns the value <tt>True</tt> if the container is full.The default version always returns <tt>False</tt>.<P>Program&nbsp;<A HREF="page120.html#progcontainerb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> also defines the properties<tt>count</tt>, <tt>isEmpty</tt> and <tt>isFull</tt>.Properties are Python object attributes that are referenced like instance attributes,but which are implemented using methods.For example,a reference to the <tt>count</tt> propertyresults in a call to the <tt>getCount</tt> method.Similarly.a reference to the <tt>isEmpty</tt> propertyresults in a call to the <tt>getIsEmpty</tt> method, anda reference to the <tt>isFull</tt> propertyresults in a call to the <tt>getIsFull</tt> method.<P>Notice that the properties are implemented as lambda expressions.The reason for this is that when a Python property is createdit is bound to a specific method instance.The problem with this is that if a propertyis bound to a specific method instance in a base class,and a derived method overrides the method,the property still remains bound to the original base class method.Thus, we would have to also override the property in the derived classin order to get the desired behavior.By binding the property to a lambda function that calls the desired method,when the method is overridden in a derived class,the property need not also be overridden in that derived class.<P><HR><A NAME="tex2html2595" HREF="page121.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2593" HREF="page118.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2589" HREF="page119.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2597" 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -