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

📄 071-075.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Using Objects for Animations</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=2//-->
<!--PAGES=071-075//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="068-071.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="075-079.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>If there&#146;s a call to a superclass constructor, it must occur at the beginning of the constructor. And if you don&#146;t invoke a superclass constructor yourself, Java does it for you! There are three rules involved:
</P>
<DL>
<DD><B>1.</B>&nbsp;&nbsp;Constructors that don&#146;t explicitly invoke a superclass constructor <I>automatically</I> invoke the superclass constructor with no arguments. In effect, Java inserts super() at the beginning of constructors that don&#146;t call a superclass constructor. For example, constructor #1 of class A has an implicit super() at its beginning. (super() in this case invokes the constructor of Object.)
<DD><B>2.</B>&nbsp;&nbsp;An exception to the previous rule occurs when the first statement of the constructor invokes another constructor, using <I>this</I>. For example, constructor #2 of class A does not have an implicit call to super().
<DD><B>3.</B>&nbsp;&nbsp;If you don&#146;t define a constructor with no arguments in a class, Java inserts one for you. This default constructor simply calls super(). For example, class A has the implicit constructor
<!-- CODE SNIP //-->
<PRE>
// implicit constructor
public A() &#123;
  super();
&#125;
</PRE>
<!-- END CODE SNIP //-->
</DL>
<P>The upshot of these rules? Each time you create an instance of an object, a sequence of constructors gets executed, from the Object class, through the inheritance hierarchy, and to the class that&#146;s instantiated.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Final Classes and Methods</FONT></H4>
<P>A <I>final</I> class is a class that can&#146;t be extended. Thus, you can&#146;t create a subclass of the following:</P>
<!-- CODE SNIP //-->
<PRE>
final class Infertile &#123;
  ...
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>A <I>final</I> method can&#146;t be overridden. For example, no subclass of Sandwich can override the applyMayonnaise() method:</P>
<!-- CODE SNIP //-->
<PRE>
class Sandwich &#123;
  final public method applyMayonnaise() &#123;
    ...
  &#125;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Now let&#146;s apply inheritance to our dancing rectangle applet!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">Using Inheritance in Our Example Applet</FONT></H4>
<P>Inheritance is appropriate for creating subclasses of DancingRect that actually know some dance steps! For example, a BoogieRect is a DancingRect; similarly, a WaltzRect is a DancingRect as well. By inheriting DancingRect&#146;s members, you&#146;ll save yourself from defining those methods and variables again for the subclasses. However, BoogieRect and WaltzRect are specializations of DancingRect, and they&#146;ll need to override the danceStep() method that they inherit in order to provide the right behavior.
</P>
<P>By overriding danceStep(), an instance of BoogieRect can perform a boogie step instead of standing still. Figure 2-10 diagrams the relationship between the three classes, and their danceStep() methods.</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/02-10.jpg',462,315 )"><IMG SRC="images/02-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-10.jpg',462,315)"><FONT COLOR="#000077"><B>Figure 2-10</B></FONT></A>&nbsp;&nbsp;DancingRect, BoogieRect, and WaltzRect classes</P>
<P>Let&#146;s look at the code to get a better idea of how method overriding is implemented. To refresh your memory, here&#146;s the definition of DancingRect again:
</P>
<!-- CODE //-->
<PRE>
class DancingRect &#123;

  int locx, locy;      // (locx,locy) are coordinates of upper
                      //    left corner of rectangle
  int width, height;   // width and height of rectangle
  Color myColor;       // color of rectangle

/////////////////////////////////////////////////////////////////
  public DancingRect(int locx,int locy,
                     int width,int height,
                     Color myColor) &#123;
    this.locx = locx;
    this.locy = locy;
    this.width = width;
    this.height = height;
    this.myColor = myColor;
  &#125;

  public void danceStep() &#123;

    // does nothing

  &#125;

  public void paint(Graphics g) &#123;
    g.setColor(myColor);
    g.fillRect(locx,locy,width,height);
  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>BoogieRect will use the &#147;dance algorithm&#148; from Broadway in its danceStep(). The full class definition of BoogieRect is shown in Listing 2-5.
</P>
<P><B>Listing 2-5</B> BoogieRect.java</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

class BoogieRect extends DancingRect &#123;

  // BoogieRect inherits all instance variables and
  // methods from DancingRect

  static final byte UP = 0;   // direction of motion
  static final byte DOWN = 1;
  static final byte LEFT = 2;
  static final byte RIGHT = 3;

  byte state;                 // state of rectangle

  int max_x;                  // max x value
  int min_x;                  // min x value
  int max_y;                  // max y value
  int min_y;                  // min y value

/////////////////////////////////////////////////////////////////

  public BoogieRect(int x,int y,int w,int h,Color c) &#123;
    super(x,y,w,h,c);         // call superclass constructor

    max_x = locx &#43; 13;
    min_x = locx - 13;
    max_y = locy &#43; 13;
    min_y = locy - 13;

 &#125;

  // override danceStep()
  // use the state machine from the Broadway applet
  public void danceStep() &#123;
    switch (state) &#123;
    case DOWN:
      locy &#43;= 2;
      if (locy &gt;= max_y) &#123;
       state = UP;
      &#125;
      break;
    case UP:
      locy -= 2;
      if (locy &lt;= min_y) &#123;
       state = RIGHT;
      &#125;
      break;
    case RIGHT:
      locx &#43;= 2;
      if (locx &gt;= max_x) &#123;
       state = LEFT;
      &#125;
      break;
    case LEFT:
      locx -= 2;
      if (locx &lt;= min_x) &#123;
       state = DOWN;
      &#125;
      break;
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>To see exactly how method overriding works, consider the following rectangles:
</P>
<!-- CODE SNIP //-->
<PRE>
DancingRect d = new DancingRect(  );
BoogieRect b = new BoogieRect(...);
</PRE>
<!-- END CODE SNIP //-->
<P>As you might expect,
</P>
<!-- CODE SNIP //-->
<PRE>
d.danceStep();
</PRE>
<!-- END CODE SNIP //-->
<P>produces a call to DancingRect&#146;s danceStep() routine, whereas
</P>
<!-- CODE SNIP //-->
<PRE>
b.danceStep();
</PRE>
<!-- END CODE SNIP //-->
<P>invokes BoogieRect&#146;s version. By contrast,
</P>
<!-- CODE SNIP //-->
<PRE>
b.paint();
</PRE>
<!-- END CODE SNIP //-->
<P>uses the paint() defined in DancingRect, since this method hasn&#146;t been overridden by BoogieRect.
</P>
<P>Now, let&#146;s give the definition of a WaltzRect. The dance motion of WaltzRect is shown in Figure 2-11.</P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/02-11.jpg',463,318 )"><IMG SRC="images/02-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-11.jpg',463,318)"><FONT COLOR="#000077"><B>Figure 2-11</B></FONT></A>&nbsp;&nbsp;Dance motion of WaltzRect<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="068-071.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="075-079.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -