📄 tij0057.html
字号:
m = new mypackage.MyClass();
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 can make this much cleaner:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">import</font> mypackage.*;
<font color="#009900">// . . . </font>
MyClass m = <font color="#0000ff">new</font> MyClass(); </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
worth keeping in mind that what the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keywords allow you to do, as a library designer, is to divide up the single
global name space so you won’t have clashing names, no matter how many
people get on the Internet and start writing classes in Java.
</FONT><a name="_Toc375545292"></a><a name="_Toc408018495"></a><P></DIV>
<A NAME="Heading163"></A><H3 ALIGN=LEFT>
Creating
unique package names
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might observe that, since a package never really gets “packaged”
into a single file, a package could be made up of many
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files, and things could get a bit cluttered. To prevent this, a logical thing
to do is to place all the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files for a particular package into a single directory; that is, use the
hierarchical file structure of the operating system to your advantage. This is
how Java handles the problem of clutter. <A NAME="Index365"></A><A NAME="Index366"></A></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
also solves two other problems: creating unique package names and finding those
classes that might be buried in a directory structure someplace. This is
accomplished, as was introduced in Chapter 2, by encoding the path of the
location of 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 into the name of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The compiler enforces this, but by convention, the first part of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
name is the Internet domain name of the creator of the class, reversed. Since
Internet domain names are guaranteed to be unique (by InterNIC,
</FONT><A NAME="fnB24" HREF="#fn24">[24]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
who controls their assignment)
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>if</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you follow this convention it’s guaranteed that your
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
name will be unique and thus you’ll never have a name clash. (That is,
until you lose the domain name to someone else who starts writing Java code
with the same path names as you did.) Of course, if you don’t have your
own domain name then you must fabricate an unlikely combination (such as your
first and last name) to create unique package names. If you’ve decided to
start publishing Java code it’s worth the relatively small effort to get
a domain name.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second part of this trick is resolving the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
name into a directory on your machine, so when the Java program runs and it
needs to <A NAME="Index367"></A><A NAME="Index368"></A>load
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
(which it does dynamically, at the point in the program where it needs to
create an object of that particular class, or the first time 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 the class), it can locate the directory where 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
resides.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
Java interpreter proceeds as follows. First, it finds the environment variable <A NAME="Index369"></A>CLASSPATH
(set via the operating system when Java, or a tool like a Java-enabled browser,
is installed on a machine). CLASSPATH contains one or more directories that are
used as roots for a search for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files. Starting at that root, the interpreter will take the package name and
replace each dot with a slash to generate a path name from the CLASSPATH root
(so
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
foo.bar.baz
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
becomes
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>foo\bar\baz
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>foo/bar/baz
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">depending
on your operating system). This is then concatenated to the various entries in
the CLASSPATH. That’s where it looks 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 with the name corresponding to the class you’re trying to create.
(It also searches some standard directories relative to where the Java
interpreter resides).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
understand this, consider my domain name, which is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bruceeckel.com</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
By reversing this,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.bruceeckel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
establishes my unique global name for my classes. (The com, edu, org, etc.
extension was formerly capitalized in Java packages, but this was changed in
Java 1.2<A NAME="Index370"></A>
so the entire package name is lowercase.) I can further subdivide this by
deciding that I want to create a library named
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so I’ll end up with a package name:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">package
com.bruceeckel.util;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
this package name can be used as an umbrella name space for the following two
files:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Vector.java</font>
<font color="#009900">// Creating a package</font>
<font color="#0000ff">package</font> com.bruceeckel.util;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Vector {
<font color="#0000ff">public</font> Vector() {
System.out.println(
"com.bruceeckel.util.Vector");
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you create your own packages, you’ll discover that 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 must be the first non-comment code in the file. The second file looks
much the same:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: List.java</font>
<font color="#009900">// Creating a package </font>
<font color="#0000ff">package</font> com.bruceeckel.util;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> List {
<font color="#0000ff">public</font> List() {
System.out.println(
"com.bruceeckel.util.List");
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Both
of these files are placed in the subdirectory on my system:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">C:\DOC\JavaT\com\bruceeckel\util</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you walk back through this, you can see the package name
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.bruceeckel.util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but what about the first portion of the path? That’s taken care of in the
CLASSPATH environment variable, which is, on my machine:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">CLASSPATH=.;D:\JAVA\LIB;C:\DOC\JavaT</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the CLASSPATH can contain a number of alternative search paths.
There’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’s located.
So for a <A NAME="Index371"></A>JAR
named
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>grape.jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
your classpath would include:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">CLASSPATH=.;D:\JAVA\LIB;C:\flavors\grape.jar</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
the classpath is set up properly, the following file can be placed in any
directory: (See page
<a name="_Hlt406577867"></a><A HREF=" PAGE#Running_programs">97</A>
if you have trouble executing this program.):
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: LibTest.java</font>
<font color="#009900">// Uses the library</font>
<font color="#0000ff">package</font> c05;
<font color="#0000ff">import</font> com.bruceeckel.util.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> LibTest {
<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();
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
the compiler encounters 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, it begins searching at the directories specified by CLASSPATH,
looking for subdirectory com\bruceeckel\util, then seeking the compiled files
of the appropriate names (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Note that both the classes and the desired methods in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must be
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
<A NAME="Heading164"></A><H4 ALIGN=LEFT>
Automatic
compilation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -