📄 093-097.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=093-097//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="090-093.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="097-100.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Protected Access</FONT></H4>
<P>The protected access level lies somewhere between public and private. Protected variables and methods are inherited, just like public members. However, protected members are visible only within a class and its subclasses.
</P>
<P>Let’s contrast protected access with its counterparts. In the following class definition, Bar is a subclass of Foo, so the protected and public members of Foo are visible within Bar. However, the private members of Foo aren’t visible in Bar.</P>
<!-- CODE //-->
<PRE>
public class Bar extends Foo {
...
public void barMethod() {
publicNumber = 17.17f; // access allowed
publicMethod(); // access allowed
protectedNumber = 13.13f; // access allowed
protectedMethod(); // access allowed
privateNumber = 9.1; // access NOT allowed
int x = privateMethod(); // access NOT allowed
Foo f = new Foo(); // instance of superclass
f.protectedNumber = 4.4f; // this is fine also
}
}
</PRE>
<!-- END CODE //-->
<P>Here’s another way of contrasting public, protected, and private. Protected access allows a programmer to <I>extend</I> the functionality of your class; public access allows others to <I>use</I> your class. Private access is for variables and methods used within the class.</P>
<P>In our Sprite class, the booleans <I>active</I> and <I>visible</I> are declared <I>protected</I> so that they’ll be visible in future subclasses of Sprite.</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Package/Default Access</FONT></H4>
<P>The <I>package</I> access level takes effect when no access modifier is used (which is why it’s the default level of access). Variables and methods at the default access level are accessible to all code throughout the package, but aren’t visible outside the package. Furthermore, the nonprivate members in a package are also visible throughout the package. Packages and package access are useful in constructing libraries of classes, and we’ll cover packages in greater detail in Chapter 10, Advanced Techniques.</P>
<P>Figure 3-2 contains a summary of the access levels that Java provides.</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/03-02.jpg',465,400 )"><IMG SRC="images/03-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-02.jpg',465,400)"><FONT COLOR="#000077"><B>Figure 3-2</B></FONT></A> Java access levels</P>
<P>Before moving on, let’s discuss one technique that’s used in conjunction with private and protected variables.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Accessor Methods</FONT></H4>
<P>Sometimes it’s necessary for an outside class to access a protected (or private) variable. Instead of making such a variable public and exposing it to the world, you can provide an <I>accessor method</I>. The methods isVisible() and setVisible(), defined in the Sprite class, are examples of accessor methods that allow other classes to test and set a protected variable.</P>
<!-- CODE SNIP //-->
<PRE>
// accessor methods:
public boolean isVisible() {
return visible;
}
public void setVisible(boolean b) {
visible = b;
}
</PRE>
<!-- END CODE SNIP //-->
<P>In a way, accessor methods allow you to have your encapsulation cake and eat it too. By providing accessor methods, you allow external clients to access a protected or private variable. At the same time, clients cannot alter such a variable directly, which preserves the benefits of encapsulation. The penalty is the additional overhead of a method call. Often, accessor methods will be declared <I>final</I> to eliminate the runtime cost of dynamic method binding.</P>
<P>Accessor methods are a common technique in object-oriented programming, and you’ll see them again and again.</P>
<P>Now you should understand what’s happening in the Sprite class. To see how this class is used, let’s rewrite the Mondrian applet we created in Chapter 1, Fundamental Java, using the Sprite class.</P>
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Applying the Sprite Class to an Example Applet</FONT></H3>
<P>Let’s look once again at the Mondrian applet we created in Chapter 1 and modified in Chapter 2. The first version was quick and dirty, the secondversion used objects, and this version will use the Sprite class. As you’ll see, the abstraction provided by Sprites enables you to reuse the applet code for sprites of any kind.
</P>
<P>The first step is to create a subclass of Sprite that displays a rectangle. This sounds like a trivial problem, but you need to create subclasses with future extensibility in mind. For example, you’ll want to derive a BitmapSprite as well as a TextSprite pretty soon. These Sprite subclasses have internal representations different from subclasses that will rely on primitives provided by java.awt.Graphics, such as RectSprite.</P>
<P>To unify the sprites based on the Graphics class primitives (like RectSprite), let’s derive another abstract class called Sprite2D, shown in Listing 3-2.</P>
<P><B>Listing 3-2</B> Sprite2D class</P>
<!-- CODE //-->
<PRE>
abstract class Sprite2D extends Sprite {
protected int locx;
protected int locy;
Color color;
boolean fill;
public boolean getFill() {
return fill;
}
public void setFill(boolean b) {
fill = b;
}
public void setColor(Color c) {
color = c;
}
public Color getColor() {
return color;
}
}
</PRE>
<!-- END CODE //-->
<P>This class introduces instance variables that track the screen location of the sprite (<I>locx</I> and <I>locy</I>), the sprite’s color, and whether it is filled or an outline. All these variables are declared <I>protected</I>, so they are directly accessible by all subclasses, but not to other clients. Sprite2D provides accessor methods to test and modify <I>color</I> and <I>fill</I>. Methods to modify <I>locx</I> and <I>locy</I> are provided in the lower subclasses.</P>
<P>RectSprite will derive from Sprite2D. Figure 3-3 shows what this class hierarchy will look like.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/03-03.jpg',315,317 )"><IMG SRC="images/03-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-03.jpg',315,317)"><FONT COLOR="#000077"><B>Figure 3-3</B></FONT></A> Current Sprite hierarchy</P>
<P>Since you’ll want to instantiate RectSprite objects, the RectSprite class must have no abstract methods. In particular, it must implement paint() and update(), which are declared by RectSprite’s grandparent, the Sprite class. Look for these methods in the definition of RectSprite, shown in Listing 3-3.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="090-093.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="097-100.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -