📄 014-016.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:Fundamental Java</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=1//-->
<!--PAGES=014-016//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="010-014.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="016-021.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Other objects will call turnKnob() to alter the state of the lamp. The turnKnob() method is the interface between the Lamp object and external objects.
</P>
<P>A Java method looks like a function definition in C, except that it occurs within the declaration of a class. Methods are called “member functions” in C++ terminology.</P>
<P>The keywords <I>public</I> and <I>private</I> are access modifiers—they specify the visibility of the variables or methods that follow. A <I>public</I> variable or method can be directly accessed by all other objects; a <I>private</I> variable or method can only be accessed by a method in a class where the <I>private</I> member is defined. You’ll learn about two other access levels—<I>protected</I> and <I>default</I>—in the following chapters.</P>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Constructors</FONT></H4>
<P>A constructor method is called to initialize an object. Constructors have the same name as the class, and they do not specify a return type. For example, the Lamp() method is a constructor that initializes the value of <I>lampIsOn</I> to <I>false</I>.</P>
<!-- CODE SNIP //-->
<PRE>
public Lamp() {
lampIsOn = false;
}
</PRE>
<!-- END CODE SNIP //-->
<P>As you see, the aspects of objects that we’ve discussed—state, behavior, and interface—have a direct mapping into Java. The state of an object is stored in one or more variables defined in the class. Similarly, behaviors correspond to methods that are defined within the class of the object. These methods are able to access and manipulate the object variables. Finally, the interface of the object is the set of methods that external objects can invoke. Thus, the functionality of an object, as expressed by its variables and methods, is defined in the class of the object.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Creating an Object</FONT></H4>
<P>Creating an object of a particular class is also known as creating an <I>instance</I> of the class, or <I>instantiating</I> an object. For example, let’s create an instance of the Lamp class.</P>
<P>First, declare a variable that refers to a Lamp object:</P>
<!-- CODE SNIP //-->
<PRE>
Lamp lamp;
</PRE>
<!-- END CODE SNIP //-->
<P>To allocate a new Lamp object that the <I>lamp</I> variable refers to, use</P>
<!-- CODE SNIP //-->
<PRE>
lamp = new Lamp();
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>new</I> keyword dynamically allocates memory for a Lamp object, and calls the Lamp() constructor for initialization.</P>
<P>Of course, you can instantiate many Lamp objects:</P>
<!-- CODE SNIP //-->
<PRE>
Lamp lamp1 = new Lamp();
Lamp lamp2 = new Lamp();
...
Lamp lamp17 = new Lamp();
</PRE>
<!-- END CODE SNIP //-->
<P>This creates 17 Lamp instances, each with its own copy of instance variable <I>lampIsOn</I>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Accessing Object Variables and Methods</FONT></H4>
<P>To refer to a variable of a given object, use the following construction (the same notation is used in C to select struct members):
</P>
<!-- CODE SNIP //-->
<PRE>
objectName.variable
</PRE>
<!-- END CODE SNIP //-->
<P>Similarly, to invoke a method of an object, use
</P>
<!-- CODE SNIP //-->
<PRE>
objectName.method();
</PRE>
<!-- END CODE SNIP //-->
<P>Here’s an example.
</P>
<P>Each lamp’s turnKnob() method can be used by other objects to send a message to that particular lamp. If any object wants to send turnKnob() messages to <I>lamp1</I> and <I>lamp13</I>, it would use the following statements:</P>
<!-- CODE SNIP //-->
<PRE>
lamp1.turnKnob(); // turn lamp1's knob
lamp13.turnKnob(); // turn lamp13's knob
</PRE>
<!-- END CODE SNIP //-->
<P>A <I>public</I> method like turnKnob(), or a <I>public</I> variable, is visible from methods of any object.</P>
<P>On the other hand, access to <I>private</I> variables and methods is restricted to the class that defines the <I>private</I> members. For example, the <I>private</I> variable <I>lampIsOn</I> can be modified by the turnKnob() method, as both are members of the Lamp class. A non-Lamp object can’t access <I>lampIsOn</I> directly, as in</P>
<!-- CODE SNIP //-->
<PRE>
lamp7.lampIsOn = true; // violates private visibility
</PRE>
<!-- END CODE SNIP //-->
<P>Now, let’s talk briefly about <I>inheritance</I>, which is another key to object-oriented design.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Inheritance</FONT></H4>
<P>You’ve inherited physical characteristics from your parents, such as hair or eye color. Similarly, a class can inherit the states and behaviors of another class. By using inheritance, you can structure relationships between classes of objects, which makes the code for your games easier to understand.
</P>
<P>One relationship that occurs often is when one class is a specialization, or refinement, of another. For example, a strobe lamp is a lamp that alternates automatically between on and off. A colored lamp is a lamp that gives light with a particular color. And all three—lamp, strobe lamp, and colored lamp—are objects.</P>
<P>Inheritance allows you to reuse code that’s been written for the Lamp class in the definitions of StrobeLamp and ColoredLamp. In particular, the StrobeLamp class can <I>inherit</I> the public variables and methods of Lamp. Put another way, the StrobeLamp class <I>extends</I> the definition of Lamp by providing additional variables and methods to the public members of Lamp. The keyword <I>extends</I> indicates inheritance in Java. Here are the definitions of Lamp and StrobeLamp:</P>
<!-- CODE //-->
<PRE>
class Lamp {
private boolean lampIsOn; // instance variable
public void turnKnob() { // method
lampIsOn = !lampIsOn;
}
public Lamp() { // constructor
lampIsOn = false;
}
}
// StrobeLamp inherits public members from Lamp
class StrobeLamp extends Lamp {
private int strobeRate; // instance variable
public setStrobeRate(int s) { // method
strobeRate = s;
}
}
</PRE>
<!-- END CODE //-->
<P>The public method turnKnob(), defined in the Lamp class, is inherited by StrobeLamp. It’s as if StrobeLamp had copied the code for turnKnob() directly in its declaration. Thus, an instance of StrobeLamp understands the message turnKnob(), even though this method isn’t defined in the interior of StrobeLamp.
</P>
<P>Similarly, ColoredLamp can inherit from Lamp. The Lamp class is called the <I>parent</I> or <I>superclass</I>, and StrobeLamp and ColoredLamp are both <I>subclasses</I>. Furthermore, the Object class (defined in the Java API) is the parent of the Lamp class. Every class in Java automatically inherits from the Object class.</P>
<P>The <I>inheritance</I> (or <I>class<I>) </I>hierarchy</I> we’ve described is shown in Figure 1-7.</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/01-07.jpg',463,317 )"><IMG SRC="images/01-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-07.jpg',463,317)"><FONT COLOR="#000077"><B>Figure 1-7</B></FONT></A> Inheritance hierarchy<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="010-014.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="016-021.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -