page602.html

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

HTML
146
字号
<HTML><HEAD><TITLE>Example-Graphical 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="tex2html8072" HREF="page603.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8070" HREF="page601.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8064" HREF="page601.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html8074" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0017621000000000000000">Example-Graphical Objects</A></H3><P>Consider a program for creating simple drawings.Suppose the program provides a set of primitive graphical objects,such as circles, rectangles, and squares.The user of the program selects the desired objects,and then invokes commands to draw, to erase, or to move them about.Ideally, all graphical objects support the same set of operations.Nevertheless,the way that the operations are implementedvaries from one object to the next.<P>We implement this as follows:We define a class that representsthe common operations provided by all graphical objects.<P>Program&nbsp;<A HREF="page602.html#proggraphicalObjecta"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>GraphicalObject</tt> class.This class provides four method,<tt>__init__</tt>, <tt>draw</tt>, <tt>erase</tt> and <tt>moveTo</tt>.<P><P><A NAME="57526">&#160;</A><A NAME="proggraphicalObjecta">&#160;</A> <IMG WIDTH=575 HEIGHT=372 ALIGN=BOTTOM ALT="program57233" SRC="img2492.gif"  ><BR><STRONG>Program:</STRONG> <tt>GraphicalObject</tt> class <tt>__init__</tt>, <tt>draw</tt>, 	<tt>erase</tt> and <tt>moveTo</tt> methods.<BR><P><P>The <tt>draw</tt> method is invoked in order to draw a graphical object.The <tt>erase</tt> method is invoked in order to erase a graphical object.The <tt>moveTo</tt> method is used to move an object to a specified positionin the drawing.The argument of the <tt>moveTo</tt> method is a <tt>Point</tt>.Program&nbsp;<A HREF="page602.html#progpointa"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Point</tt> classwhich represents a position in a drawing.<P><P><A NAME="57532">&#160;</A><A NAME="progpointa">&#160;</A> <IMG WIDTH=575 HEIGHT=122 ALIGN=BOTTOM ALT="program57259" SRC="img2493.gif"  ><BR><STRONG>Program:</STRONG> <tt>Point</tt> class <tt>__init__</tt> method.<BR><P><P>The <tt>GraphicalObject</tt> class has a single instance attribute,<tt>_center</tt>,which is a <tt>Point</tt> that represents the position in a drawingof the center-point of the graphical object.In addition to <tt>self</tt>,the <tt>__init__</tt> method for the <tt>GraphicalObject</tt> class takesas its argument a <tt>Point</tt> andinitializes the <tt>_center</tt> instance attribute accordingly.<P>Program&nbsp;<A HREF="page602.html#proggraphicalObjecta"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows a possible implementationfor the <tt>erase</tt> method:In this case we assume that the image is drawn using an imaginary pen.Assuming that we know how to draw a graphical object,we can erase the object by changing the color of the penso that it matches the background colorand then redrawing the object.<P>Once we can erase an object as well as draw it,then moving it is easy.Just erase the object,change its <tt>_center</tt> point,and then draw it again.This is how the <tt>moveTo</tt> method shown inProgram&nbsp;<A HREF="page602.html#proggraphicalObjecta"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is implemented.<P>We have seen that the <tt>GraphicalObject</tt> class providesimplementations for the <tt>Erase</tt> and <tt>MoveTo</tt> methods.However, the <tt>GraphicalObject</tt> class does not providean implementation for the <tt>Draw</tt> method.Instead, the method is declared to be <tt>abstract</tt>.We do this because until we know what kind of object it is,we cannot possibly know how to draw it!<P>Consider the <tt>Circle</tt> class defined in Program&nbsp;<A HREF="page602.html#progcirclea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>Circle</tt> class <em>extends</em><A NAME=57292>&#160;</A>the <tt>GraphicalObject</tt> class.Therefore, it inherits the instance attribute <tt>_center</tt>and the methods <tt>erase</tt> and <tt>moveTo</tt>.The <tt>circle</tt> class adds an additional instance attribute,<tt>_radius</tt>,and it overrides the <tt>draw</tt> method.The body of the <tt>draw</tt> method is not shown in Program&nbsp;<A HREF="page602.html#progcirclea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.However, we shall assume that it draws a circle with the given radiusand center point.<P><P><A NAME="57535">&#160;</A><A NAME="progcirclea">&#160;</A> <IMG WIDTH=575 HEIGHT=161 ALIGN=BOTTOM ALT="program57302" SRC="img2494.gif"  ><BR><STRONG>Program:</STRONG> <tt>Circle</tt> class.<BR><P><P>Notice the way the <tt>__init__</tt>method of the <tt>Circle</tt> class is implemented.This method first calls the <tt>__init__</tt> method ofthe <em>superclass</em><A NAME=57317>&#160;</A> of class <tt>Circle</tt>,that is <tt>GraphicalObject</tt>.The <tt>GraphicalObject</tt> <tt>__init__</tt> method initializes the <tt>_center</tt> attribute.Then the <tt>Circle</tt> <tt>__init__</tt> method initializesthe <tt>_radius</tt> attribute.<P>Using the <tt>Circle</tt> class defined in Program&nbsp;<A HREF="page602.html#progcirclea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>we can write code like this:<PRE>c = Circle(Point(0, 0), 5)c.draw()c.moveTo(Point(10, 10))c.erase()</PRE>This code sequence declares a circle object with its centerinitially at position (0,0) and radius&nbsp;5.The circle is then drawn, moved to (10,10), and then erased.<P>Program&nbsp;<A HREF="page602.html#progrectanglea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Rectangle</tt> classand Program&nbsp;<A HREF="page602.html#progsquarea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Square</tt> class.The <tt>Rectangle</tt> class also extends the <tt>GraphicalObject</tt> class.Therefore, it inherits the instance attribute <tt>_center</tt>and the methods <tt>erase</tt> and <tt>moveTo</tt>.The <tt>Rectangle</tt> class adds two additional instance attributes,<tt>_height</tt> and <tt>_width</tt>,and it overrides the <tt>draw</tt> method.The body of the <tt>draw</tt> method is not shown in Program&nbsp;<A HREF="page602.html#progrectanglea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.However, we shall assume that it draws a rectangle with the given dimensionsand center point.<P><P><A NAME="57539">&#160;</A><A NAME="progrectanglea">&#160;</A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program57343" SRC="img2495.gif"  ><BR><STRONG>Program:</STRONG> <tt>Rectangle</tt> class.<BR><P><P>The <tt>Square</tt> class extends the <tt>Rectangle</tt> class.No new instance attributes or methods are declared--those inherited from <tt>GraphicalObject</tt> or from<tt>Rectangle</tt> are sufficient.The <tt>__init__</tt> method simply arranges to make sure that the <tt>_height</tt>and <tt>_width</tt> of a square are equal!<P><P><A NAME="57543">&#160;</A><A NAME="progsquarea">&#160;</A> <IMG WIDTH=575 HEIGHT=88 ALIGN=BOTTOM ALT="program57361" SRC="img2496.gif"  ><BR><STRONG>Program:</STRONG> <tt>Square</tt> class.<BR><P><HR><A NAME="tex2html8072" HREF="page603.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8070" HREF="page601.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8064" HREF="page601.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html8074" 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 + -
显示快捷键?