📄 071-075.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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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’s a call to a superclass constructor, it must occur at the beginning of the constructor. And if you don’t invoke a superclass constructor yourself, Java does it for you! There are three rules involved:
</P>
<DL>
<DD><B>1.</B> Constructors that don’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’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> 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> If you don’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() {
super();
}
</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’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’t be extended. Thus, you can’t create a subclass of the following:</P>
<!-- CODE SNIP //-->
<PRE>
final class Infertile {
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>A <I>final</I> method can’t be overridden. For example, no subclass of Sandwich can override the applyMayonnaise() method:</P>
<!-- CODE SNIP //-->
<PRE>
class Sandwich {
final public method applyMayonnaise() {
...
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>Now let’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’s members, you’ll save yourself from defining those methods and variables again for the subclasses. However, BoogieRect and WaltzRect are specializations of DancingRect, and they’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> DancingRect, BoogieRect, and WaltzRect classes</P>
<P>Let’s look at the code to get a better idea of how method overriding is implemented. To refresh your memory, here’s the definition of DancingRect again:
</P>
<!-- CODE //-->
<PRE>
class DancingRect {
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) {
this.locx = locx;
this.locy = locy;
this.width = width;
this.height = height;
this.myColor = myColor;
}
public void danceStep() {
// does nothing
}
public void paint(Graphics g) {
g.setColor(myColor);
g.fillRect(locx,locy,width,height);
}
}
</PRE>
<!-- END CODE //-->
<P>BoogieRect will use the “dance algorithm” 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 {
// 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) {
super(x,y,w,h,c); // call superclass constructor
max_x = locx + 13;
min_x = locx - 13;
max_y = locy + 13;
min_y = locy - 13;
}
// override danceStep()
// use the state machine from the Broadway applet
public void danceStep() {
switch (state) {
case DOWN:
locy += 2;
if (locy >= max_y) {
state = UP;
}
break;
case UP:
locy -= 2;
if (locy <= min_y) {
state = RIGHT;
}
break;
case RIGHT:
locx += 2;
if (locx >= max_x) {
state = LEFT;
}
break;
case LEFT:
locx -= 2;
if (locx <= min_x) {
state = DOWN;
}
break;
}
}
}
</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’s danceStep() routine, whereas
</P>
<!-- CODE SNIP //-->
<PRE>
b.danceStep();
</PRE>
<!-- END CODE SNIP //-->
<P>invokes BoogieRect’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’t been overridden by BoogieRect.
</P>
<P>Now, let’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> 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 + -