⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 025-029.html

📁 java game programming e-book
💻 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,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</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=025-029//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="021-025.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="029-032.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Appendix A, Java Reference Tables, contains a complete listing of Java operators.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Control Flow</FONT></H4>
<P>Java supports the control structures found in C: <I>if...else</I>, <I>for</I>, <I>while</I>, <I>do...while</I>, and <I>switch</I>. Listing 1-2 gives an illustration of each of these.</P>
<P><B>Listing 1-2</B> Java control structures</P>
<!-- CODE //-->
<PRE>
class ControlFlow &#123;
  public static void main(String argv[]) &#123;

    /* for loop */
    /* prints numbers from 0 to 17 */

    for (int i=0; i&lt;17; i&#43;&#43;) &#123;
      System.out.println(i);
    &#125;

    /* while loop */
    /* steps through charArray elements */
    char charArray[] = &#123;'r','i','a','I','n'&#125;;
    int j = 0;

    while (j &lt; charArray.length) &#123;
      System.out.println(charArray[j&#43;&#43;]);
    &#125;
    /* do...while loop */
    /* prints 3
              2
              1 */
    int k = 3;
    do &#123;
      System.out.println(k);
      k--;
    &#125;
    while (k != 0);

    /* switch */
    /* prints "case 13" */
    int s = 13;
    switch (s) &#123;
      case 3:
        System.out.println("case 3");
        break;
      case 13:
        System.out.println("case 13");
        break;
      default:
        System.out.println("default case");
        break;
    &#125;

    /* if...else */
    /* prints "Bye" */
    if (j - s == 17) &#123;
      System.out.println("Hello");
    &#125;
    else &#123;
      System.out.println("Bye");
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>As you see, the control structures in Java are practically identical to the ones in C!
</P>
<P>Java also eliminates <I>goto</I>, and adds labeled <I>break</I> and <I>continue</I> statements that allow you to get out of nested blocks. Here&#146;s an example of the use of a labeled break statement:</P>
<!-- CODE //-->
<PRE>
find:                     // this is a label that
                          //   refers to the following
                          //   statement
  for (int i=0; i&lt;13; i&#43;&#43;) &#123;
    for (int j=0; j&lt;17; j&#43;&#43;) &#123;
      if (i*j == 1713)
        break find;       // exit out of statement
                          //   labelled 'find'
    &#125;
  &#125;

// execution resumes here if break is executed
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Threads</FONT></H4>
<P>In the real world, many different things happen at the same time; similarly, there are many programming situations where it&#146;s necessary to simulate multiple, concurrent processes. This is done in Java with <I>threads</I>. Think of threads as subprograms that execute independently of one another, and yet can work together as well. Threads are a powerful tool for creating simulations. We&#146;ll cover threads in detail in Chapters 8, Implementing a High Score Server on a Network, and 10, Advanced Techniques.</P>
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Exceptions</FONT></H4>
<P>An <I>exception</I> indicates a condition that is out of the ordinary, such as an error. When a method detects an error, it can <I>throw</I> an exception. An exception handler can <I>catch</I> the thrown exception. Here&#146;s a quick example:</P>
<!-- CODE //-->
<PRE>
try &#123;
  // execute the code in here.
  // if exception occurs, flow of control
  //     jumps to catch block
  // else finish executing try block
&#125;
catch (Exception e) &#123;
  // code in here is executed if exception occurs
&#125;
finally &#123;
  // code in here is ALWAYS executed, after try block
  //     and/or catch block are executed
&#125;
</PRE>
<!-- END CODE //-->
<P>Java&#146;s powerful exception handling mechanism is based on the <I>try-catch</I> construct found in C&#43;&#43;. You&#146;ll see exception handling at work starting with the next chapter; Chapter 8, Implementing a High Score Server on a Network, will cover it in depth.</P>
<H4 ALIGN="CENTER"><A NAME="Heading29"></A><FONT COLOR="#000077">Major Differences Between Java, C, and C&#43;&#43;</FONT></H4>
<P>Here are some of the major differences between Java, C, and C&#43;&#43;:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Java has no pointers</I>. However, this doesn&#146;t prevent you from implementing data structures that use pointers, such as linked lists, because an object (or array) variable is a reference handle to object (or array) contents. You&#146;ll see an illustration of this program in Listing 1-5. Also, remember that objects and arrays are passed by reference.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Java has no global variables and no global functions</I>. All variables and methods must appear within a class declaration. Putting a method in a class declaration doesn&#146;t imply inline expansion, as it does in C&#43;&#43;.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Java supports method overloading</I>. The same method name may be used multiple times in a class declaration to denote distinct methods, as long as the argument lists or return types are different. You can define many methods named foo() in the same class, as long as each one has a different method signature.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Java has no preprocessor</I>. Thus, there are no macros (<I>#define</I>) or built-in facilities for conditional compilation (<I>#if </I>and <I>#ifdef</I>) as provided in C. In addition, Java doesn&#146;t use function prototypes or header files, so it doesn&#146;t need <I>#include</I>.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>In Java, instance (nonstatic) methods are bound dynamically by default</I>. In other words, Java methods behave like virtual functions in C&#43;&#43;.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>In Java, a String is an object, and not an array of char</I>. In addition, a String object is <I>immutable</I>, which means that you can&#146;t change its contents. Strings are discussed in Chapter 4, Adding Interactivity.
</DL>
<P>The designers of Java wanted to create a simpler, more orthogonal language than either C or C&#43;&#43;, so they eliminated several features. Table 1-1 enumerates some features that have been eliminated, and their Java substitutes.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 1-1</B> C/C&#43;&#43; features eliminated from Java
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="40%" ALIGN="LEFT">C/C&#43;&#43; Feature
<TH WIDTH="60%" ALIGN="LEFT" VALIGN="BOTTOM">Java Substitute
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>#<I>define</I> for constants
<TD>Static final variables
<TR>
<TD><I>#define</I> for macros and other preprocessor directives (e.g., <I>#</I>, <I>##</I>, <I>#ifdef</I>)
<TD VALIGN="TOP">None
<TR>
<TD>Enumerated types
<TD>Static final variables
<TR>
<TD>Function not defined in a class
<TD>Static method
<TR>
<TD>Multiple inheritance
<TD>Multiple interfaces
<TR>
<TD>Operator overloading
<TD>None
<TR>
<TD>Struct or union or typedef
<TD>Class
<TR>
<TD>Templates
<TD>None
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="021-025.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="029-032.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -