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

📄 tij307.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>public</font> <font color=#0000ff>class</font> List {
  <font color=#0000ff>public</font> List() {
    System.out.println(<font color=#004488>"com.bruceeckel.simple.List"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Both of these files are placed in the subdirectory on my system: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_834" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>C:\DOC\JavaT\com\bruceeckel\simple</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>If you walk back through this, you can see the package name <b>com.bruceeckel.simple</b>, but what about the first portion of the path? That&#146;s taken care of in the CLASSPATH environment variable, which is, on my machine: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_835" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>CLASSPATH=.;D:\JAVA\LIB;C:\DOC\JavaT</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You can see that the CLASSPATH can contain a number of alternative search paths. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_836" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>There&#146;s a variation when using JAR files, however. You must put the name of the JAR file in the classpath, not just the path where it&#146;s located. So for a JAR named <a name="Index433"></a><b>grape.jar</b> your classpath would include:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>CLASSPATH=.;D:\JAVA\LIB;C:\flavors\grape.jar</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Once the classpath is set up properly, the following file can be placed in any directory:<br></p>

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

<font color=#0000ff>public</font> <font color=#0000ff>class</font> LibTest {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Vector v = <font color=#0000ff>new</font> Vector();
    List l = <font color=#0000ff>new</font> List();
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"com.bruceeckel.simple.Vector"</font>,
      <font color=#004488>"com.bruceeckel.simple.List"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>When the compiler encounters the <b>import</b> statement for the <b>simple</b> library, it begins searching at the directories specified by CLASSPATH, looking for subdirectory com\bruceeckel\simple, then seeking the compiled files of the appropriate names (<b>Vector.class</b> for <b>Vector</b>, and <b>List.class</b> for <b>List</b>). Note that both the classes and the desired methods in <b>Vector</b> and <b>List</b> must be <b>public</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_837" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Setting the CLASSPATH has been such a trial for beginning Java users (it was for me, when I started) that Sun made the JDK in Java 2 a bit smarter. You&#146;ll find that when you install it, even if you don&#146;t set the CLASSPATH, you&#146;ll be able to compile and run basic Java programs. To compile and run the source-code package for this book (available at <i>www.BruceEckel.com</i>), however, you will need to add the base directory of the book&#146;s code tree to your CLASSPATH. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_838" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading4719"></a>Collisions</h4>
<p>What happens if two libraries are imported via &#145;<b>*</b>&#146; and they include the same names? For example, suppose a program does this:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.bruceeckel.simple.*;
<font color=#0000ff>import</font> java.util.*;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Since <b>java.util.*</b> also contains a <b>Vector</b> class, this causes a potential collision. However, as long as you don&#146;t write the code that actually causes the collision, everything is OK&#151;this is good, because otherwise you might end up doing a lot of typing to prevent collisions that would never happen. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_839" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The collision <i>does</i> occur if you now try to make a <b>Vector</b>:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Vector v = <font color=#0000ff>new</font> Vector();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Which <b>Vector</b> class does this refer to? The compiler can&#146;t know, and the reader can&#146;t know either. So the compiler complains and forces you to be explicit. If I want the standard Java <b>Vector</b>, for example, I must say:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>java.util.Vector v = <font color=#0000ff>new</font> java.util.Vector();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Since this (along with the CLASSPATH) completely specifies the location of that <b>Vector</b>, there&#146;s no need for the <b>import java.util.*</b> statement unless I&#146;m using something else from <b>java.util</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_840" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545293"></a><a name="_Toc24775622"></a><a name="Heading4732"></a>A
custom tool library</h3>
<p>With this knowledge, you can now create your own libraries of tools to reduce or eliminate duplicate code. Consider, for example, creating an alias for <b>System.out.println(&#160;)</b> to reduce typing. This can be part of a package called <b>tools</b>:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:tools:P.java</font>
<font color=#009900>// The P.rint &amp; P.rintln shorthand.</font>
<font color=#0000ff>package</font> com.bruceeckel.tools;

<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(String s) {
    System.out.print(s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> rintln(String s) {
    System.out.println(s);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You can use this shorthand to print a <b>String</b> either with a newline (<b>P.rintln(&#160;)</b>) or without a newline (<b>P.rint(&#160;)</b>). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_841" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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:<br></p>

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

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ToolTest {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <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>);
    P.rintln(<font color=#004488>""</font> + 100); <font color=#009900>// Force it to be a String</font>
    P.rintln(<font color=#004488>""</font> + 100L);
    P.rintln(<font color=#004488>""</font> + 3.14159);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Available from now on!"</font>,
      <font color=#004488>"100"</font>,
      <font color=#004488>"100"</font>,
      <font color=#004488>"3.14159"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Notice that all objects can easily be forced into <b>String</b> representations by putting them in a <b>String </b>expression; in the preceding example, starting the expression with an empty <b>String </b>does the trick. But this brings up an interesting observation. If you call <b>System.out.println(100)</b>, it works without casting it to a <b>String</b>. With some extra overloading, you can get the <b>P</b> class to do this as well (this is an exercise at the end of this chapter). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_842" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>So from now on, whenever you come up with a useful new utility, you can add it to your own <b>tools</b> or <b>util </b>directory. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_843" title="Send BackTalk Comment">Feedback</a></font><br></p>

⌨️ 快捷键说明

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