page593.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 46 行
HTML
46 行
<HTML><HEAD><TITLE>Example-Complex Numbers</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="tex2html7968" HREF="page594.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7966" HREF="page591.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7960" HREF="page592.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7970" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0017420000000000000000">Example-Complex Numbers</A></H2><P>Suppose we wish to define a class to represent<em>complex numbers</em><A NAME=56943> </A>.One way to do this is to define a class called <tt>Complex</tt>that uses two instance attributes,<tt>_real</tt> and <tt>_imag</tt>,to represent the real and imaginary parts of a complex number (respectively)<A NAME="tex2html1126" HREF="footnode.html#57490"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/foot_motif.gif"></A>.The <tt>Complex</tt> class definition shown in Program <A HREF="page593.html#progcomplexa"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>illustrates how this can be done.<P><P><A NAME="57491"> </A><A NAME="progcomplexa"> </A> <IMG WIDTH=575 HEIGHT=142 ALIGN=BOTTOM ALT="program56950" SRC="img2485.gif" ><BR><STRONG>Program:</STRONG> <tt>Complex</tt> class <tt>__init__</tt> method.<BR><P><P>Every instance of the <tt>Complex</tt> classcontains its own instance attributes.Consider the following statements:<PRE>c = Complex(1, 0)d = Complex(2, 0)</PRE>Both <tt>c</tt> and <tt>d</tt> refer to distinct instancesof the <tt>Complex</tt> class.Therefore,each of them has its own <tt>_real</tt> and <tt>_imag</tt> instance attribute.The instance attributes of an object are accessed using the <em>dot</em> operator.For example,<tt>c._real</tt> refers to the <tt>_real</tt> instance attribute of <tt>c</tt>and <tt>d._imag</tt> refers to the <tt>_imag</tt> instance attribute of <tt>d</tt>.<P><HR><A NAME="tex2html7968" HREF="page594.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7966" HREF="page591.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7960" HREF="page592.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7970" 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 + -
显示快捷键?