chapter05.html

来自「java 是一个很好的网络开发环境。由于它是通过解释的方法」· HTML 代码 · 共 1,008 行 · 第 1/5 页

HTML
1,008
字号
<font color=#0000ff>public</font> <font color=#0000ff>class</font> P {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(Object obj) {
    System.out.print(obj);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(String s) {
    System.out.print(s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>char</font>[] s) {
    System.out.print(s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>char</font> c) {
    System.out.print(c);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>int</font> i) {
    System.out.print(i);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>long</font> l) {
    System.out.print(l);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>float</font> f) {
    System.out.print(f);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>double</font> d) {
    System.out.print(d);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rint(<font color=#0000ff>boolean</font> b) {
    System.out.print(b);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln() {
    System.out.println();
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(Object obj) {
    System.out.println(obj);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(String s) {
    System.out.println(s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>char</font>[] s) {
    System.out.println(s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>char</font> c) {
    System.out.println(c);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>int</font> i) {
    System.out.println(i);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>long</font> l) {
    System.out.println(l);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>float</font> f) {
    System.out.println(f);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>double</font> d) {
    System.out.println(d);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(<font color=#0000ff>boolean</font> b) {
    System.out.println(b);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All the different data types can
now be printed out either with a newline (<B>P.rintln(&#160;)</B>) or without a
newline (<B>P.rint(&#160;)</B>).<A NAME="AAASpellcheck"></A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can guess that the location of
this file must be in a directory that starts at one of the CLASSPATH locations,
then continues <B>com/bruceeckel/tools</B>. After compiling, the <B>P.class</B>
file can be used anywhere on your system with an <B>import</B>
statement:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: ToolTest.java</font>
<font color=#009900>// Uses the tools library</font>
<font color=#0000ff>import</font> com.bruceeckel.tools.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ToolTest {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    P.rintln(<font color=#004488>"Available from now on!"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So from now on, whenever you come
up with a useful new utility, you can add it to the <B>tools</B> directory. (Or
to your own personal <B>util</B> or <B>tools</B> directory.)</FONT><BR></P></DIV>
<A NAME="Heading166"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Classpath pitfall</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>P.java</B> file brought up
an interesting pitfall. Especially with early implementations of Java, setting
the classpath correctly is generally quite a headache. During the development of
this book, the <B>P.java</B> file was introduced and seemed to work fine, but at
some point it began breaking. For a long time I was certain that this was the
fault of one implementation of Java or another, but finally I discovered that at
one point I had introduced a program (<B>CodePackager.java</B>, shown in Chapter
17)<B> </B>that used a different class <B>P</B>. Because it was used as a tool,
it was <I>sometimes</I> placed in the classpath, and other times it
wasn&#8217;t. When it was, the <B>P</B> in <B>CodePackager.java </B>was found
first by Java when executing a program in which it was looking for the class in
<B>com.bruceeckel.tools</B>, and the compiler would say that a particular method
couldn&#8217;t be found. This was frustrating because you can see the method in
the above class <B>P</B> and no further diagnostics were reported to give you a
clue that it was finding a completely different class. (That wasn&#8217;t even
<B>public.</B>)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At first this could seem like a
compiler bug, but if you look at the <B>import</B> statement it says only
&#8220;here&#8217;s where you <I>might</I> find <B>P</B>.&#8221; However, the
compiler is supposed to look anywhere in its classpath, so if it finds a
<B>P</B> there it will use it, and if it finds the &#8220;wrong&#8221; one
<I>first</I> during a search then it will stop looking. This is slightly
different from the case described earlier, because there the offending classes
were both in packages, and here there was a <B>P</B> that was not in a package,
but could still be found during a normal classpath search.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you&#8217;re having an
experience like this, check to make sure that there&#8217;s only one class of
each name anywhere in your
<A NAME="Index376"></A>classpath.</FONT><A NAME="_Toc375545294"></A><A NAME="_Toc408018497"></A><BR></P></DIV>
<A NAME="Heading167"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using imports to change behavior</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A feature that is missing from Java
is C&#8217;s <I>conditional compilation</I>, which allows you to change a switch
and get different behavior without changing any other code. The reason such a
feature was left out of Java is probably because it is most often used in C to
solve cross-platform issues: different portions of the code are compiled
depending on the platform that the code is being compiled for. Since Java is
intended to be automatically cross-platform, such a feature should not be
necessary.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, there are other valuable
uses for conditional compilation. A very common use is for debugging code. The
debugging features are enabled during development, and disabled for a shipping
product. Allen Holub (www.holub.com) came up with the idea of using packages to
mimic conditional compilation. He used this to create a Java version of
C&#8217;s very useful <I>assertion mechanism</I>, whereby you can say
&#8220;this should be true&#8221; or &#8220;this should be false&#8221; and if
the statement doesn&#8217;t agree with your assertion you&#8217;ll find out
about it. Such a tool is quite helpful during debugging.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here is the class that you&#8217;ll
use for debugging:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Assert.java</font>
<font color=#009900>// Assertion tool for debugging</font>
<font color=#0000ff>package</font> com.bruceeckel.tools.debug;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Assert {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> perr(String msg) {
    System.err.println(msg);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_true(<font color=#0000ff>boolean</font> exp) {
    <font color=#0000ff>if</font>(!exp) perr(<font color=#004488>"Assertion failed"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_false(<font color=#0000ff>boolean</font> exp){
    <font color=#0000ff>if</font>(exp) perr(<font color=#004488>"Assertion failed"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_true(<font color=#0000ff>boolean</font> exp, String msg) {
    <font color=#0000ff>if</font>(!exp) perr(<font color=#004488>"Assertion failed: "</font> + msg);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_false(<font color=#0000ff>boolean</font> exp, String msg) {
    <font color=#0000ff>if</font>(exp) perr(<font color=#004488>"Assertion failed: "</font> + msg);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This class simply encapsulates
boolean tests, which print error messages if they fail. In Chapter 9,
you&#8217;ll learn about a more sophisticated tool for dealing with errors
called <I>exception handling</I>, but the <B>perr(&#160;)</B> method will work
fine in the meantime.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you want to use this class,
you add a line in your program:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.bruceeckel.tools.debug.*;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To remove the assertions so you can
ship the code, a second <B>Assert</B> class is created, but in a different
package:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Assert.java</font>
<font color=#009900>// Turning off the assertion output </font>
<font color=#009900>// so you can ship the program.</font>
<font color=#0000ff>package</font> com.bruceeckel.tools;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Assert {
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_true(<font color=#0000ff>boolean</font> exp){}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_false(<font color=#0000ff>boolean</font> exp){}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_true(<font color=#0000ff>boolean</font> exp, String msg) {}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_false(<font color=#0000ff>boolean</font> exp, String msg) {}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now if you change the previous
<B>import</B> statement to:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.bruceeckel.tools.*;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The program will no longer print
out assertions. Here&#8217;s an example:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: TestAssert.java</font>
<font color=#009900>// Demonstrating the assertion tool</font>
<font color=#0000ff>package</font> c05;
<font color=#009900>// Comment the following, and uncomment the</font>
<font color=#009900>// subsequent line to change assertion behavior:</font>
<font color=#0000ff>import</font> com.bruceeckel.tools.debug.*;
<font color=#009900>// import com.bruceeckel.tools.*;</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> TestAssert {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Assert.is_true((2 + 2) == 5);
    Assert.is_false((1 + 1) == 2);
    Assert.is_true((2 + 2) == 5, <font color=#004488>"2 + 2 == 5"</font>);

⌨️ 快捷键说明

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