📄 064-068.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=064-068//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="060-064.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="068-071.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The keyword <I>this</I> is necessary in the constructor for DancingRect to distinguish the local variables (which are temporary) from instance variables <I>of the same name</I> (which provide storage for the object).</P>
<P>Here’s another example of <I>this</I>, taken from the Broadway animation applet above.</P>
<!-- CODE SNIP //-->
<PRE>
animation = new Thread(this);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Thread</I> constructor takes an object as its argument. By passing <I>this</I>, the Broadway applet object passes a reference to itself as the argument.</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Using this in Constructors</FONT></H4>
<P>There’s one more way that <I>this</I> is used. Methods, and constructors in particular, can be overloaded. In other words, a class can have several constructors, as long as each one has a distinct signature (i.e., argument list). When <I>this</I>, followed by an argument list, is the first statement in a constructor, the appropriate constructor is called. For example, look at the following class, which has two constructors that invoke each other:</P>
<!-- CODE //-->
<PRE>
class A {
// constructor #1
public A(int x, float y) {
this(y); // invoke constructor #2
...
}
// constructor #2
public A(float x) {
this((int)x, x); // invoke constructor #1
...
}
}
</PRE>
<!-- END CODE //-->
<P>As you see, the <I>this</I> statement must occur at the beginning of each constructor. this() (<I>this</I> with no arguments) calls the constructor with no arguments.</P>
<P>Being able to invoke another constructor can save you a bit of typing, if you’ve written one constructor that does all the work and you want another way of calling it.</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Using the DancingRect Class</FONT></H4>
<P>Now let’s go back to the DancingRect class. We can create an instance of DancingRect by calling the constructor in the following manner:
</P>
<!-- CODE SNIP //-->
<PRE>
// create an instance of DancingRect at (80,80)
// with width 40 and height 40
DancingRect r = new DancingRect(80,80,40,40);
</PRE>
<!-- END CODE SNIP //-->
<P>To tell <I>r</I> to perform a dance step and paint itself, use the following syntax:</P>
<!-- CODE SNIP //-->
<PRE>
r.danceStep(); // make a dance step
r.paint(); // paint r to screen
</PRE>
<!-- END CODE SNIP //-->
<P>Let’s use the DancingRect class to implement a new version of the Mondrian class from Chapter 1. In Listing 2-4 you can immediately see how information is now distributed, handled, and encapsulated by the objects that need it, instead of being accessible to all.
</P>
<P><B>Listing 2-4</B> Rebuilt Mondrian.java</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
// rebuilt Mondrian with objects
public class Mondrian2 extends Applet {
static final int NUM_RECTS = 9;
DancingRect r[]; // array of dancing rectangles
public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
initRectangles();
}
public void initRectangles() {
// allocate dancing rectangles
// now the data is encapsulated by the objects!
r = new DancingRect[NUM_RECTS];
r[0] = new DancingRect(0,0,90,90,Color.yellow);
r[1] = new DancingRect(250,0,40,190,Color.yellow);
r[2] = new DancingRect(200,55,60,135,Color.yellow);
r[3] = new DancingRect(80,200,220,90,Color.blue);
r[4] = new DancingRect(100,10,90,80,Color.blue);
r[5] = new DancingRect(80,100,110,90,Color.lightGray);
r[6] = new DancingRect(200,0,45,45,Color.red);
r[7] = new DancingRect(0,100,70,200,Color.red);
r[8] = new DancingRect(200,55,60,135,Color.magenta);
}
public void start() {
System.out.println(">> start <<");
}
public void paint(Graphics g) {
for (int i=0; i<NUM_RECTS; i++) {
r[i].paint(g); // paint each rectangle
}
}
public void stop() {
System.out.println(">> stop <<");
}
}
</PRE>
<!-- END CODE //-->
<P>This new Mondrian is not only cleaner and easier to understand, it is also easily extensible, as you’ll see soon. To extend Mondrian, you’re going to define dancing rectangles that <I>actually</I> dance, using inheritance.</P>
<H3><A NAME="Heading16"></A><FONT COLOR="#000077">Using Inheritance</FONT></H3>
<P>In Chapter 1, Fundamental Java, you learned some of the basics of inheritance, and you’ve been using inheritance to create applets. Now we will explore inheritance in greater detail.
</P>
<P>Inheritance allows you to reuse class definitions for creating new classes. Inheritance is signified by the <I>extends</I> keyword, as in the following:</P>
<!-- CODE SNIP //-->
<PRE>
class foo extends bar {
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, the foo class inherits bar’s public variables and methods. An instance of foo will be able to use a public method or variable defined in the bar class, as if it were actually defined by foo. (Protected variables and methods are also inherited, and you’ll learn about <I>protected</I> access in the next chapter.)</P>
<P>The foo class is said to be the <I>subclass</I> or <I>derived</I> <I>class</I>; the bar class is the <I>parent</I> <I>class</I>, <I>base class</I>, or <I>superclass</I>. Look at Figure 2-9 for a diagram of what happens in the inheritance.</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/02-09.jpg',465,398 )"><IMG SRC="images/02-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-09.jpg',465,398)"><FONT COLOR="#000077"><B>Figure 2-9</B></FONT></A> foo inherits public variables and methods from bar</P>
<P>As you can imagine, being able to reuse code in this manner can be a huge time-saver. Another advantage of inheritance is that changes to the behavior of a superclass are automatically propagated to its subclasses, which can cut down on code development time. But for inheritance to be a time-saver that makes code extensible and manageable, it must be used in the proper way. There are situations when it is needed, and times when it is inappropriate.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">When to Use Inheritance</FONT></H4>
<P>Inheritance is appropriate when there is an “is-a” relationship between two classes. To determine if such a relationship exists between two classes A and B, create the sentence, “A is a B.” If this sentence makes sense, then inheritance is called for, with A as the subclass, and B as the superclass.
</P>
<P>For example, a human is a mammal, so a Human class would properly inherit from a Mammal class. Similarly, a Computer is an ElectricPoweredDevice, so the first class would inherit from the second one. These are examples of <I>extension</I> relationships, since the subclass adds extra characteristics to the parent class. In other words, a human has all the characteristics of a mammal, plus intelligence, speaking ability, and a few other noteworthy features.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="060-064.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="068-071.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -