📄 087-089.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:Animating Sprites</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=3//-->
<!--PAGES=087-089//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="083-087.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="090-093.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Using Abstract Classes</FONT></H3>
<P>A class stores information about state and behavior. The classes that you have seen are templates from which you can create, or <I>instantiate</I>, actual objects. By contrast, an <I>abstract class</I> is a class in which no instantiation is allowed. Let’s see why abstract classes are useful.</P>
<P>Consider the problem of creating classes that describe physical features of dinosaurs, for use in a Dinosaur battle game. Particular dinosaur types, such as the Triceratops, Stegosaurus, and the infamous Tyrannosaurus Rex, are all deserving of their own classes, since each has distinct physical characteristics that distinguish it and make it dangerous or vulnerable to attack. A Triceratops, for example, is a powerful foe, armed with three horns and a shield on its head. On the other hand, in a battle game, a Bronto-saurus is a definite liability, with a long, humped body, a long neck, and a preference for leafy greens. Moreover, each class has features common to all dinosaurs, such as tough, reptilian skin, cold blood, and intelligence worthy of an earthworm. These essential dinosaur features properly belong to a parent class called Dinosaur.</P>
<P>Let’s briefly sketch what the Triceratops, Brontosaurus, and Dinosaur classes might look like in our game:</P>
<!-- CODE //-->
<PRE>
public class Dinosaur {
byte brainMass; // in milligrams
short weight; // in kilograms
int scalySkinStrength;
boolean plantEater;
boolean meatEater;
...
}
public class Triceratops extends Dinosaur {
short hornLength[] = new int[3]; // array of horn lengths
short shieldStrength;
...
}
public class BrontosaurusStegaosauraus extends Dinosaur {
short humpSize;
short neckLength;
...
}
</PRE>
<!-- END CODE //-->
<P>Figure 3-1 illustrates the relationship between the three classes.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/03-01.jpg',461,315 )"><IMG SRC="images/03-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-01.jpg',461,315)"><FONT COLOR="#000077"><B>Figure 3-1</B></FONT></A> Dinosaur hierarchy</P>
<P>Now, here’s the dilemma. It is possible to create Triceratops and Brontosaurus objects with these definitions for use in the game. But you can also instantiate a Dinosaur object. This should <I>not</I> be possible, since the Dinosaur class doesn’t specify an actual object in our game, but the characteristics common to all dinosaur objects.</P>
<P>The solution is simple. Declare Dinosaur to be an <I>abstract class</I>, by using the <I>abstract</I> keyword as follows:</P>
<!-- CODE SNIP //-->
<PRE>
public abstract class Dinosaur {
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>Now no instantiation of Dinosaur is possible:
</P>
<!-- CODE SNIP //-->
<PRE>
Dinosaur d = new Dinosaur(); // illegal for abstract class
</PRE>
<!-- END CODE SNIP //-->
<P>Abstract classes usually sit at the top of class hierarchies and often correspond to categories of objects that are so broad, in the scope of the problem, that further refinement is needed before instantiation is possible. For example, classes such as Mammal, Musician, and ElectricPoweredDevice, discussed in the previous chapter, might best be represented by abstract classes.
</P>
<P>Methods can be abstract as well. Abstract methods serve as placeholders for behaviors that subclasses can implement. For example, behaviors common to all dinosaurs are eating and sleeping. This could be specified in the Dinosaur class in our game as follows:</P>
<!-- CODE SNIP //-->
<PRE>
public abstract class Dinosaur {
...
// methods:
public abstract void eat();
public abstract void sleep();
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>Now the abstract methods can be brought to life by the Triceratops class, for example:
</P>
<!-- CODE //-->
<PRE>
public class Triceratops extends Dinosaur {
...
// methods:
public void eat() {
System.out.println("Triceratops eating");
...
}
public void sleep() {
System.out.println("Triceratops sleeping");
...
}
}
</PRE>
<!-- END CODE //-->
<P>If a subclass of Dinosaur doesn’t implement these abstract methods, it must be declared abstract. Put another way, any class that defines or inherits abstract methods (which remain unimplemented) must be declared abstract, or else an error results. Abstract methods correspond to pure virtual functions in C++, and they are called <I>deferredmethods</I> in other object-oriented languages.</P>
<P>Now let’s see how abstract classes can help us in defining the root of our sprite hierarchy.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="083-087.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="090-093.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -