📄 016-021.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=016-021//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="014-016.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="021-025.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
</P>
<P>Using objects and inheritance effectively is really important to writing games in Java, and we’ll flesh out these concepts in the next few chapters. This section introduced many new terms (and there are a few more coming!), so here’s a glossary to help you out.
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD>
<P><FONT SIZE="+1"><B>Object-Oriented Terminology</B></FONT></P>
<DL>
<DT><B><TT><I>behavior</I></TT>
</B>
<DD>What an object does. Usually corresponds to a verb (driving, attacking, laughing).
<DT><B><TT><I>class</I></TT>
</B>
<DD>A template for creating objects. Consists of a collection of variable and method definitions.
<DT><B><TT><I>class hierarchy</I></TT>
</B>
<DD>The inheritance relationships between classes.
<DT><B><TT><I>constructor</I></TT>
</B>
<DD>A method that has the same name as the class. It initializes a new object.
<DT><B><TT><I>encapsulation</I></TT>
</B>
<DD>Hiding information (data and methods) from the outside.
<DT><B><TT><I>extends</I></TT>
</B>
<DD>A Java keyword that indicates inheritance.
<DT><B><TT><I>inheritance</I></TT>
</B>
<DD>Creating new classes by reusing code from existing classes.
<DT><B><TT><I>instance</I></TT>
</B>
<DD>An object.
<DT><B><TT><I>instance variable/method</I></TT>
</B>
<DD>A variable/method associated with an object.
<DT><B><TT><I>interface</I></TT>
</B>
<DD>The members of an object accessible from the outside.
<DT><B><TT><I>message</I></TT>
</B>
<DD>An invocation of a method.
<DT><B><TT><I>method</I></TT>
</B>
<DD>A function that occurs in a class declaration.
<DT><B><TT><I>object</I></TT>
</B>
<DD>A bundle of data and methods.
<DT><B><TT><I>parent class</I></TT>
</B>
<DD>Same as superclass.
<DT><B><TT><I>private</I></TT>
</B>
<DD>An access modifier that restricts visibility to members of the class.
<DT><B><TT><I>public</I></TT>
</B>
<DD>An access modifier that permits all access.
<DT><B><TT><I>state</I></TT>
</B>
<DD>The modes an object can exist in. Usually corresponds to a noun or adjective (hungry, sad, energy level).
<DT><B><TT><I>static variable/method</I></TT>
</B>
<DD>A variable/method associated with a class.
<DT><B><TT><I>subclass</I></TT>
</B>
<DD>If X inherits from Y, X is a subclass of Y.
<DT><B><TT><I>superclass</I></TT>
</B>
<DD>If X inherits from Y, Y is the superclass of X.
</DL>
</TABLE>
<P>Now it’s time for a summary of the Java language and environment.
</P>
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Java Summary</FONT></H3>
<P>Let’s summarize the basic aspects of Java. This is a brief, bird’s-eye view to get you started as quickly as possible, so don’t worry if you don’t catch everything. The details will be fleshed out in the following chapters.
</P>
<P>The Java system can be divided into three major components:</P>
<DL>
<DD><B>•</B> The Java core. This consists of the base language and fundamental classes you’ll use in your programs.
<DD><B>•</B> The Java API. These are the standard libraries of classes that you may use with your programs.
<DD><B>•</B> The Java interpreter. This loads, verifies, and executes your program.
</DL>
<P>The interplay of these components gives Java many of its benefits. Let’s examine each component in turn.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">The Java Core</FONT></H4>
<P>Java’s syntax resembles C and C++. However, the syntactic similarities belie some important semantic differences. In the following, we’ll briefly cover
</P>
<DL>
<DD><B>•</B> Data types. We’ll discuss primitive types, arrays, classes, and objects.
<DD><B>•</B> Instance, static, and final variables and methods.
<DD><B>•</B> Memory management. Java’s garbage collector frees unused memory for you.
<DD><B>•</B> Packages. These are groups of classes.
<DD><B>•</B> Operators. Java uses most of the operators found in C.
<DD><B>•</B> Control flow. Java’s control flow constructs are practically the same as C’s.
<DD><B>•</B> Advanced features. Java includes support for threads and exceptions.
<DD><B>•</B> Major differences between Java, C, and C++.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Primitive Data Types</FONT></H4>
<P>In addition to the basic types found in C, Java supports <I>boolean</I> and <I>byte</I> types. Listing 1-1 shows the syntax of the primitive types, which looks reassuringly like C.</P>
<P><B>Listing 1-1</B> Primitive types in Java</P>
<!-- CODE //-->
<PRE>
/* this is a comment */
// this is also a comment
class PrimitiveTypes {
/* integer types */
byte b = 37; // signed 8 bits
short s = 7; // signed 16 bits
int i = 2013; // signed 32 bit
long l = 200213L; // signed 64 bits; trailing 'l' or
// 'L' indicates a long literal
/* floating point types */
float f = 232.42f; // 32 bits; trailing 'f' or 'F'
// indicates a float literal
double d = 177.77; // 64 bits; without a 'f' means
// a double literal
/* boolean type */
boolean t = true; // true and false are the only allowable
boolean f = false; // values allowed for a boolean.
// Casts between booleans and numeric types
are not allowed.
/* character type */
char c = 'J'; // unsigned 16 bit Unicode character
}
</PRE>
<!-- END CODE //-->
<P>These primitive data types are <I>passed by value</I> to Java methods. In other words, when a variable of a primitive type is passed to a method, its value is copied to the formal parameter. This is illustrated in a sample program (Listing 1-4).</P>
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Arrays</FONT></H4>
<P>To create an array, you need to declare a variable that refers to the array, and then allocate space for the array using the <I>new</I> keyword.</P>
<P>For example, here’s how to declare a variable that refers to an array of int:</P>
<!-- CODE SNIP //-->
<PRE>
int intArray[];
</PRE>
<!-- END CODE SNIP //-->
<P>or
</P>
<!-- CODE SNIP //-->
<PRE>
int[] intArray;
</PRE>
<!-- END CODE SNIP //-->
<P><I>intArray</I> is a <I>reference</I> to an array of integers. The default value for an array variable such as <I>intArray</I> is <I>null</I>, which indicates an absence of reference. Figure 1-8 shows what an unallocated array looks like.</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/01-08.jpg',315,249 )"><IMG SRC="images/01-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-08.jpg',315,249)"><FONT COLOR="#000077"><B>Figure 1-8</B></FONT></A> Unallocated array</P>
<P>To allocate space for an array, use the <I>new</I> keyword:</P>
<!-- CODE SNIP //-->
<PRE>
intArray = new int[17]; // array of 17 integers
</PRE>
<!-- END CODE SNIP //-->
<P>Now the <I>intArray</I> variable refers to the allocated memory, as shown in Figure 1-9.</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/01-09.jpg',462,399 )"><IMG SRC="images/01-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/01-09.jpg',462,399)"><FONT COLOR="#000077"><B>Figure 1-9</B></FONT></A> Allocated array</P>
<P>Another way of creating an array is by explicitly initializing it, using the usual C syntax:
</P>
<!-- CODE SNIP //-->
<PRE>
char charArray[] = { 'r', 'n', 'a', 'I', 'I'};
</PRE>
<!-- END CODE SNIP //-->
<P>Arrays are 0-indexed, and individual array elements are accessed using subscript notation:
</P>
<!-- CODE SNIP //-->
<PRE>
intArray[7] = 13; // assign 13 to the 8th element of intArray
</PRE>
<!-- END CODE SNIP //-->
<P>Array accesses are checked for legality at runtime; attempting to access array elements that are out of bounds throws an <I>exception</I>.</P>
<P>Every array has a <I>length</I> field, which stores the size of the array. For example, the length of <I>intArray</I> is</P>
<!-- CODE SNIP //-->
<PRE>
intArray.length
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="014-016.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="021-025.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -