📄 tij0057.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0056.html">Prev</a> | <a href="tij0058.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
package:
the library unit
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
<A NAME="Index353"></A>package
is what you get when you use the <A NAME="Index354"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword to bring in an entire library, such as
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
java.util.*;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
brings in the entire utility <A NAME="Index355"></A>library
that’s part of the standard Java distribution. Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you can now either specify the full name
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util.Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which you can do without the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement), or you can simply say
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(because of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</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
you want to bring in a single class, you can name that class in the
</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><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
java.util.Vector;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
you can use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with no qualification. However, none of the other classes in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are available.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
reason for all this importing is to provide a mechanism to manage “<A NAME="Index356"></A>name
spaces.” The names of all your class members are insulated from each
other. A method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>f( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside a class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will not <A NAME="Index357"></A>clash
with an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>f( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that has the same signature (argument list) in class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>B</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
But what about the class names? Suppose you create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class that is installed on a machine that already has a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class that’s written by someone else? With Java on the Internet, this can
happen without the user knowing it since classes can get downloaded
automatically in the process of running a Java program.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
potential clashing of names is why it’s important to have complete
control over the name spaces in Java, and to be able to create a completely
unique name regardless of the constraints of the Internet.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
far, most of the examples in this book have existed in a single file and have
been designed for local use, and haven’t bothered with package names. (In
this case the class name is placed in the “default package.”) This
is certainly an option, and for simplicity’s sake this approach will be
used whenever possible throughout the rest of the book. If you’re
planning to create a program that is “Internet friendly,” however,
you must think about preventing class name clashes.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you create a source-code file for Java, it’s commonly called a <A NAME="Index358"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>compilation
unit
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(sometimes a <A NAME="Index359"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>translation
unit
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Each compilation unit must have a name ending in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and inside the compilation unit there can be a public class that must have the
same name as the file (including capitalization, but excluding the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
filename extension). If you don’t do this, the compiler will complain.
There can be only
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>
one
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
<A NAME="Index360"></A><A NAME="Index361"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class in each compilation unit (again, the compiler will complain). The rest of
the classes in that compilation unit, if there are any, are hidden from the
world outside that package because they’re
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and they comprise “support” classes for the main
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you compile 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 you get an output file with exactly the same name but an extension of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>for
each class in the
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file. Thus you can end up with quite a few
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files from a small number of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files. If you’ve programmed with a compiled language, you might be used
to the compiler spitting out an intermediate form (usually an “obj”
file) that is then packaged together with others of its kind using a linker (to
create an executable file) or a librarian (to create a library). That’s
not how Java works. A working program is a bunch of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files, which can be packaged and compressed into a <A NAME="Index362"></A><A NAME="Index363"></A>JAR
file (using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">utility
in Java 1.1<A NAME="Index364"></A>).
The Java interpreter is responsible for finding, loading and interpreting these
files.
</FONT><A NAME="fnB23" HREF="#fn23">[23]</A><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
library is also a bunch of these class files. Each file has one class that is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(you’re not forced to have a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class, but it’s typical), so there’s one component for each file.
If you want to say that all these components (that are in their own separate
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">files)
belong together, that’s where the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword comes in.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">package
mypackage;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">at
the beginning of a file, where the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">statement
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>must
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">appear
as the first non-comment in the file, you’re stating that this
compilation unit is part of a library named
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mypackage</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Or, put another way, you’re saying that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class name within this compilation unit is under the umbrella of the name
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mypackage</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and if anyone wants to use the name they must either fully specify the name or
use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword in combination with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mypackage</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(using the choices given previously). Note that the convention for Java
packages is to use all lowercase letters, even for intermediate words.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
example, suppose the name of the file is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MyClass.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This means there can be one and only one
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class in that file, and the name of that class must be
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MyClass</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(including the capitalization):
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">package</font> mypackage;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> MyClass { </TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black"> <font color="#009900">// . . . </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now,
if someone wants to use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MyClass</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or, for that matter, any of the other
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mypackage</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
they must use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword to make the name or names in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mypackage</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
available. The alternative is to give the fully-qualified name:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">mypackage.MyClass
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -