📄 021-025.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=021-025//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="016-021.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="025-029.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Since an array variable is a reference to array contents, these contents can be modified when the array is passed to a Java method. For example, in the following method call:
</P>
<!-- CODE SNIP //-->
<PRE>
myclass.method(intArray);
</PRE>
<!-- END CODE SNIP //-->
<P>the contents of <I>intArray</I> can be modified in the body of myclass.method(). You’ll see an example of this in Listing 1-4.</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Classes and Objects</FONT></H4>
<P>As you’ve seen above, the concepts of class and object go hand in hand. A class declares a bundle of data and methods that are associated with the class name; an object is an instance of a class. When you declare a class, you’re creating a new type that can be used in variable declarations. To create an object of a given class, declare a variable that refers to the object, and then allocate space for the object using the <I>new</I> keyword. (This process is analogous to creating a Java array.)</P>
<P>For example, consider the following class declaration:</P>
<!-- CODE //-->
<PRE>
class Foo {
int x = 0; // instance variable
int add(int a) { // instance method
x += a;
}
static float y; // class variable
static void minus() { // class method
x -= 1;
}
public Foo(float z) { // constructor
y = z;
}
}
</PRE>
<!-- END CODE //-->
<P>Now you can declare a variable that refers to a Foo object:
</P>
<!-- CODE SNIP //-->
<PRE>
Foo f;
</PRE>
<!-- END CODE SNIP //-->
<P>The default value for an object variable is <I>null</I>, which means that the variable doesn’t refer to anything (yet). Here’s how to create the object:</P>
<!-- CODE SNIP //-->
<PRE>
f = new Foo(13.0); // allocation and initialization
</PRE>
<!-- END CODE SNIP //-->
<P>This statement allocates memory for a Foo object, calls the Foo() constructor with the given argument, and sets <I>f</I> to refer to the new object. The process of declaring and allocating an object is shown in Figure 1-10.</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/01-10.jpg',461,316 )"><IMG SRC="images/01-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-10.jpg',461,316)"><FONT COLOR="#000077"><B>Figure 1-10</B></FONT></A> Declaring and allocating an object</P>
<P>As with array variables, object variables are actually reference handles to the allocated object. Thus, a method can alter the contents of an object that is passed to it.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Instance, Static, and Final Variables and Methods</FONT></H4>
<P>Look at the definition of the Foo class again. The variable <I>x</I> is an <I>instance variable</I>, which means that each object of type Foo has its own copy of <I>x</I>. The <I>static</I> keyword in the declaration of <I>y</I> makes it a <I>class</I> or <I>static variable</I>. A static variable exists whether or not the class has been instantiated. Furthermore, there is only one copy of a static variable, regardless of how many instances of the class you create. This distinction is mirrored in the way you access these variables:</P>
<!-- CODE SNIP //-->
<PRE>
f.x = 13; // objectName.instanceVariable
Foo.y = 17.0f; // className.staticVariable
</PRE>
<!-- END CODE SNIP //-->
<P>In other words, to access an instance variable, you need an instance of the class. To access a class variable, prefix the variable with the class name. Figure 1-11 illustrates the distinction between an instance and a class variable in the Foo class.
</P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/01-11.jpg',465,399 )"><IMG SRC="images/01-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-11.jpg',465,399)"><FONT COLOR="#000077"><B>Figure 1-11</B></FONT></A> Comparing instance and class variables of Foo</P>
<P>Methods can also be declared static; in this case, they are known as <I>class</I> or <I>static methods</I>, and can be invoked even if there aren’t any instances of the class around. Instance methods, on the other hand, require the presence of an object to be invoked. Again, the syntax is illustrative:</P>
<!-- CODE SNIP //-->
<PRE>
f.add(3); // objectName.instanceMethod();
Foo.minus(); // className.staticMethod();
</PRE>
<!-- END CODE SNIP //-->
<P>A variable that is declared <I>final</I> is a constant. A variable declared both <I>static</I> and <I>final</I> (a static, final variable) is a constant that is a static variable. In the following:</P>
<!-- CODE SNIP //-->
<PRE>
class Constants {
static final double PI = 3.14159;
}
</PRE>
<!-- END CODE SNIP //-->
<P><I>Constants.PI</I> is a static final variable. Methods and classes can also be declared <I>final</I>; you’ll learn about this in the next chapter.</P>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Memory Management</FONT></H4>
<P>In Java, memory is dynamically allocated with the <I>new</I> keyword. But unlike C++, there isn’t a <I>delete</I> operator to free allocated memory that is not needed. This is because Java’s runtime system automatically garbage-collects memory no longer in use. You don’t need to worry about freeing memory you have finished using.</P>
<P>Sometimes an object holds system resources that the garbage collector doesn’t keep track of, such as file descriptors or sockets. In this case, you should <I>finalize</I> the object before it’s garbage collected, by providing a finalize() method in the definition of the class. You’ll see how to do this in Chapter 8, Implementing a High Score Server on a Network.</P>
<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Packages</FONT></H4>
<P>A package is a group of classes that share related functionality or purpose. Each package has a name that identifies it. A package name consists of one or more names separated by periods, such as java.awt.image. To refer to a given class in a package, prefix the name of the class with the package name, and separate the two names by a period. For example, the Graphics class in the package java.awt is java.awt.Graphics. The combination of the package name and class name is known as the <I>fully qualified name</I> of the class.</P>
<P>All classes in Java belong to some package. Within a class definition, you can refer to another class in the same package by using the class name alone. However, a class must use the fully qualified name when referring to a class outside of its own package. Otherwise, you can <I>import</I> the class in your program. By importing a class, you can refer to it with the class name alone.</P>
<P>We’ll start using packages in our first applet, and Chapter 10, Advanced Techniques, covers them in depth.</P>
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Operators</FONT></H4>
<P>Java uses most of the C operators you’re familiar with, but eliminates
</P>
<DL>
<DD><B>•</B> Dereferencing operators: * and ->
<DD><B>•</B> Address operator: &
<DD><B>•</B> <I>sizeof</I>
<DD><B>•</B> Comma operator: ,
<BR>However, Java adds a few operators as well. The most notable additions:
<DD><B>•</B> <I>instanceof</I> tests to see if an object is an instance of a class. More precisely,
<!-- CODE SNIP //-->
<PRE>
object instanceof class
</PRE>
<!-- END CODE SNIP //-->
<BR>returns <I>true</I> if <I>object</I> is an instance of <I>class</I>, and <I>false</I> otherwise. If <I>object</I> is null, <I>false</I> is returned.
<DD><B>•</B> >>> is an additional operator that denotes logical right shift. In other words, the vacated bits are filled with zeroes. >>>= performs an assignment along with the shift. The >> and >>= operators perform arithmetic right shifts (i.e., the vacated bits are filled with the sign bit).
<DD><B>•</B> + and += are overloaded to perform String concatenation. You’ll learn about Strings in Chapter 4, Adding Interactivity.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="016-021.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="025-029.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -