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

📄 chap05.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I10' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I11>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A library is also a bunch of these class
files. Each file has one class that is <B>public</B> (you&#8217;re not forced to
have a <B>public</B> class, but it&#8217;s typical), so there&#8217;s one
component for each file. If you want to say that all these components (that are
in their own separate <B>.java </B>and <B>.class </B>files) belong together,
that&#8217;s where the <B>package</B> keyword comes in.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I11' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you say:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>package</font> mypackage;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">at the beginning of a file (if you use a
<B>package </B>statement, it <I>must </I>appear as the first noncomment in the
file), you&#8217;re stating that this compilation unit is part of a library
named <B>mypackage</B>. Or, put another way, you&#8217;re saying that the
<B>public</B> class name within this compilation unit is under the umbrella of
the name <B>mypackage</B>, and if anyone wants to use the name they must either
fully specify the name or use the <B>import</B> keyword in combination with
<B>mypackage</B> (using the choices given previously). Note that the convention
for Java package names is to use all lowercase letters, even for intermediate
words. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I12' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For example, suppose the name of the file
is <B>MyClass.java</B>. This means there can be one and only one <B>public</B>
class in that file, and the name of that class must be <B>MyClass</B> (including
the capitalization):</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>package</font> mypackage;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> MyClass {
  <font color=#009900>// . . .</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now, if someone wants to use
<B>MyClass</B> or, for that matter, any of the other <B>public</B> classes in
<B>mypackage</B>, they must use the <B>import</B> keyword to make the name or
names in <B>mypackage</B> available. The alternative is to give the fully
qualified name:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>mypackage.MyClass m = <font color=#0000ff>new</font> mypackage.MyClass();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>import</B> keyword can make this
much cleaner:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> mypackage.*;
<font color=#009900>// . . . </font>
MyClass m = <font color=#0000ff>new</font> MyClass();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It&#8217;s worth keeping in mind that
what the <B>package</B> and <B>import</B> keywords allow you to do, as a library
designer, is to divide up the single global name space so you won&#8217;t have
clashing names, no matter how many people get on the Internet and start writing
classes in Java.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I13' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I14>
</FONT><A NAME="_Toc375545292"></A><A NAME="_Toc481064590"></A><BR></P></DIV>
<A NAME="Heading191"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Creating unique package names</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might observe that, since a package
never really gets &#8220;packaged&#8221; into a single file, a package could be
made up of many <B>.class</B> files, and things could get a bit cluttered. To
prevent this, a logical thing to do is to place all the <B>.class</B> 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 one way that Java
references the problem of clutter; you&#8217;ll see the other way later when the
<B>jar</B> utility is introduced. <A NAME="Index486"></A><A NAME="Index487"></A>

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I14' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I15>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Collecting the package files into a
single subdirectory 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 <B>.class</B> file into the name of the
<B>package</B>. The compiler enforces this, but by convention, the first part of
the <B>package</B> name is the Internet domain name of the creator of the class,
reversed. Since Internet domain names are guaranteed to be unique, <I>if</I> you
follow this convention it&#8217;s guaranteed that your <B>package</B> name will
be unique and thus you&#8217;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&#8217;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&#8217;ve decided to start
publishing Java code it&#8217;s worth the relatively small effort to get a
domain name. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I15' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I16>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The second part of this trick is
resolving the <B>package</B> name into a directory on your machine, so when the
Java program runs and it needs to
<A NAME="Index488"></A><A NAME="Index489"></A>load the <B>.class </B>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 <B>static
</B>member of the class), it can locate the directory where the <B>.class
</B>file resides. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I16' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java interpreter proceeds as follows.
First, it finds the environment variable <A NAME="Index490"></A>CLASSPATH (set
via the operating system, sometimes by the installation program that installs
Java or a Java-based tool on your machine). CLASSPATH contains one or more
directories that are used as roots for a search for <B>.class</B> 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
<B>package foo.bar.baz</B> becomes <B>foo\bar\baz </B>or <B>foo/bar/baz </B>or
possibly something else, depending on your operating system). This is then
concatenated to the various entries in the CLASSPATH. That&#8217;s where it
looks for the <B>.class</B> file with the name corresponding to the class
you&#8217;re trying to create. (It also searches some standard directories
relative to where the Java interpreter resides).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I17' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I18>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To understand this, consider my domain
name, which is <B>bruceeckel.com</B>. By reversing this, <B>com.bruceeckel</B>
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 2 so the entire package name is lowercase.) I can further subdivide this by
deciding that I want to create a library named <B>simple</B>, so I&#8217;ll end
up with a package name:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>package</font> com.bruceeckel.simple;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now this package name can be used as an
umbrella name space for the following two files:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I18' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I19>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:simple:Vector.java</font>
<font color=#009900>// Creating a package.</font>
<font color=#0000ff>package</font> com.bruceeckel.simple;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Vector {
  <font color=#0000ff>public</font> Vector() {
    System.out.println(
      <font color=#004488>"com.bruceeckel.util.Vector"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you create your own packages,
you&#8217;ll discover that the <B>package</B> statement must be the first
noncomment code in the file. The second file looks much the same:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I19' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I20>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:simple:List.java</font>
<font color=#009900>// Creating a package.</font>
<font color=#0000ff>package</font> com.bruceeckel.simple;

<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.util.List"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Both of these files are placed in the
subdirectory on my system: 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I20' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I21>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>C:\DOC\JavaT\com\bruceeckel\simple</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;s taken care of in the CLASSPATH environment
variable, which is, on my machine:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I21' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I22>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>CLASSPATH=.;D:\JAVA\LIB;C:\DOC\JavaT</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that the CLASSPATH can
contain a number of alternative search paths. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I22' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I23>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;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&#8217;s located. So for a <A NAME="Index491"></A>JAR named
<B>grape.jar</B> your classpath would include:</FONT><BR></P></DIV>

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

<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.simple.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> LibTest {

⌨️ 快捷键说明

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