📄 tij0057.html
字号:
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first time you create an object of an imported class (or you access a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member of a class), the compiler will hunt for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file of the same name (so if you’re creating an object of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it looks for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
in the appropriate directory. If it finds only
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
X.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
that’s what it must use. However, if it also finds an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the same directory, the compiler will compare the date stamp on the two
files, and if
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is more recent than
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it will <A NAME="Index372"></A><A NAME="Index373"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>automatically
recompile
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B><I>.</I></B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to generate an up-to-date
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
a class is not in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file of the same name as that class, this behavior will not occur for that class.
</FONT><P></DIV>
<A NAME="Heading165"></A><H4 ALIGN=LEFT>
Collisions<a name="collisions_name"></a></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
happens if two libraries are imported via * and they include the same <A NAME="Index374"></A><A NAME="Index375"></A>names?
For example, suppose a program does this:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">import</font> com.bruceeckel.util.*;
<font color="#0000ff">import</font> java.util.*; </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util.*</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also contains a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class, this causes a potential collision. However, as long as the collision
doesn’t actually occur, everything is OK – this is good because
otherwise you might end up doing a lot of typing to prevent collisions that
would never happen.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
collision
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>does</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occur if you now try to make a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Vector
v = new Vector();
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Which
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class does this refer to? The compiler can’t know, and the reader
can’t know either. So the compiler complains and forces you to be
explicit. If I want the standard Java
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
for example, I must say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">java.util.Vector
v = new java.util.Vector();
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
this (along with the CLASSPATH) completely specifies the location of that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
there’s no need for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import
java.util.*
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement unless I’m using something else from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545293"></a><a name="_Toc408018496"></a><P></DIV>
<A NAME="Heading166"></A><H3 ALIGN=LEFT>
A
custom tool library
<a name="COM_EckelObjects_tools"></a></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out.println( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to reduce typing. This can be part of a package called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: P.java</font>
<font color="#009900">// The P.rint & 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(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">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
the different data types can now be printed out either with a newline (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>P.rintln( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
or without a newline (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>P.rint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).<a name="AAASpellcheck"></a></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can guess that the location of this file must be in a directory that starts at
one of the CLASSPATH locations, then continues
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com/bruceeckel/tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
After compiling, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>P.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file can be used anywhere on your system with an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement:
</FONT><P></DIV>
<font color="#990000"><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("Available from now on!");
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
from now on, whenever you come up with a useful new utility, you can add it to
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directory. (Or to your own personal
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directory.)
</FONT><P></DIV>
<A NAME="Heading167"></A><H4 ALIGN=LEFT>
Classpath
pitfall
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>P.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>P.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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 (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -