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

📄 page595.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
字号:
<HTML><HEAD><TITLE>Properties, Accessors and Mutators</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="tex2html7990" HREF="page596.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7988" HREF="page591.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7982" HREF="page594.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7992" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0017440000000000000000">Properties, Accessors and Mutators</A></H2><P>Program&nbsp;<A HREF="page595.html#progcomplexb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> continues the definition of the <tt>Complex</tt> class.It defines four methods,<tt>getReal</tt>, <tt>setReal</tt>, <tt>getImag</tt> and <tt>setImage</tt>,as well as two properties,<tt>real</tt> and <tt>imag</tt>.<P><P><A NAME="57497">&#160;</A><A NAME="progcomplexb">&#160;</A> <IMG WIDTH=575 HEIGHT=409 ALIGN=BOTTOM ALT="program56995" SRC="img2486.gif"  ><BR><STRONG>Program:</STRONG> <tt>Complex</tt> class <tt>real</tt> and <tt>imag</tt> properties.<BR><P><P>The <tt>getReal</tt> and <tt>setImag</tt> methodsprovide the means to access the <tt>_real</tt> and <tt>_imag</tt>instance attributes, respectively.A method that accesses an instance but does not modify that instance is calledan <em>accessor</em><A NAME=57020>&#160;</A>.Therefore, <tt>getReal</tt> and <tt>setReal</tt> are accessors.<P>The <tt>setReal</tt> and <tt>setImag</tt> methodsprovide the means to modify the <tt>_real</tt> and <tt>_imag</tt>instance attributes, respectively.A method that modifies an instance is calleda <em>mutator</em><A NAME=57028>&#160;</A>.Therefore, <tt>setReal</tt> and <tt>setImage</tt> are mutators.<P>The <em>dot</em> operator is used to specify the object on whicha method is to be invoked.For example,the sequence of statements<PRE>c.setReal(2)print c.getReal()</PRE>is equivalent to<PRE>Complex.setReal(c, 2)print Complex.getReal(c)</PRE><P>Program&nbsp;<A HREF="page595.html#progcomplexb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> also definestwo properties called <tt>real</tt> and <tt>imag</tt>.<P>A Python property is an class attributethat provides an accessor method and/or a mutator method.The <tt>fget</tt> argument of a property specifiesan accessor method called the ``<em>getter</em><A NAME=57037>&#160;</A>''and the <tt>fset</tt> argument of a property specifiesa mutator method called the ``<em>setter</em><A NAME=57040>&#160;</A>''.<P>For example,in Program&nbsp;<A HREF="page595.html#progcomplexb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the <tt>real</tt> property getter is the <tt>getReal</tt> accessor methodand the <tt>real</tt> property setter is the <tt>setReal</tt> mutator method.Similarly,the <tt>imag</tt> property getter is <tt>getImag</tt>and the <tt>imag</tt> property setter is <tt>setReal</tt>.<P>Properties let you use instance attribute notationrather than method call notationto invoke an accessor or a mutator method.Again, the <em>dot</em> operator is used to specify the object on whichthe operation is performed.For example,the sequence of statements<PRE>c.imag = 2print c.imag</PRE>is equivalent to<PRE>Complex.setImag(c, 2)print Complex.getImag(c)</PRE><P>Program&nbsp;<A HREF="page595.html#progcomplexc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> definesfour methods and two more properties of the <tt>Complex</tt> class.The <tt>r</tt> propertyuses the <tt>getR</tt> accessor as its getterand the <tt>setR</tt> mutator as its setter.Similarly, the <tt>theta</tt> propertyuses the <tt>getTheta</tt> accessor as its getterand the <tt>setTheta</tt> mutator as its setter.<P><P><A NAME="57504">&#160;</A><A NAME="progcomplexc">&#160;</A> <IMG WIDTH=575 HEIGHT=543 ALIGN=BOTTOM ALT="program57059" SRC="img2487.gif"  ><BR><STRONG>Program:</STRONG> <tt>Complex</tt> class <tt>r</tt> and <tt>theta</tt> properties.<BR><P><P>By defining suitable properties,it is possible to hide the implementation of the classfrom the user of that class.Consider the following statements:<PRE>print c._realprint c.real</PRE>The first statement depends on the implementation of the <tt>Complex</tt> class.If we change the implementation of the class fromthe one given (which uses rectangular coordinates)to one that uses polar coordinates,then the first statement above must also be changed.On the other hand,the second statement does not need to be modified,provided we reimplement the <tt>real</tt> propertywhen we switch to polar coordinates.<P><HR><A NAME="tex2html7990" HREF="page596.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7988" HREF="page591.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7982" HREF="page594.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7992" 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 + -