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

📄 chap05.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>package</font> com.bruceeckel.tools;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Assert {
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_true(<font color=#0000ff>boolean</font> exp){}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> is_false(<font color=#0000ff>boolean</font> exp){}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_true(<font color=#0000ff>boolean</font> exp, String msg) {}
  <font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  is_false(<font color=#0000ff>boolean</font> exp, String msg) {}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now if you change the previous
<B>import</B> statement to:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.bruceeckel.tools.*;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The program will no longer print
assertions. Here&#8217;s an example:

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

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

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c05:TestAssert.java</font>
<font color=#009900>// Demonstrating the assertion tool.</font>
<font color=#009900>// Comment the following, and uncomment the</font>
<font color=#009900>// subsequent line to change assertion behavior:</font>
<font color=#0000ff>import</font> com.bruceeckel.tools.debug.*;
<font color=#009900>// import com.bruceeckel.tools.*;</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> TestAssert {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Assert.is_true((2 + 2) == 5);
    Assert.is_false((1 + 1) == 2);
    Assert.is_true((2 + 2) == 5, <font color=#004488>"2 + 2 == 5"</font>);
    Assert.is_false((1 + 1) == 2, <font color=#004488>"1 +1 != 2"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">By changing the <B>package</B>
that&#8217;s imported, you change your code from the debug version to the
production version. This technique can be used for any kind of conditional code.

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

<backtalk:display ID=TIJ3_CHAPTER5_I37>
</FONT><A NAME="_Toc481064593"></A><BR></P></DIV>
<A NAME="Heading195"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Package caveat</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It&#8217;s worth remembering that anytime
you create a package, you implicitly specify a
<A NAME="Index494"></A><A NAME="Index495"></A>directory structure when you give
the package a name. The package <I>must</I> live in the directory indicated by
its name, which must be a directory that is searchable starting from the
CLASSPATH. Experimenting with the <B>package</B> keyword can be a bit
frustrating at first, because unless you adhere to the package-name to
directory-path rule, you&#8217;ll get a lot of mysterious run-time messages
about not being able to find a particular class, even if that class is sitting
there in the same directory. If you get a message like this, try commenting out
the <B>package</B> statement, and if it runs you&#8217;ll know where the problem
lies.

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

<backtalk:display ID=TIJ3_CHAPTER5_I38>
</FONT><A NAME="_Toc375545295"></A><A NAME="_Toc481064594"></A><BR></P></DIV>
<A NAME="Heading196"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Java access specifiers</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When used, the Java
<A NAME="Index496"></A><A NAME="Index497"></A>access specifiers
<A NAME="Index498"></A><B>public</B>, <A NAME="Index499"></A><B>protected,</B>
and <A NAME="Index500"></A><B>private</B> are placed in front of each definition
for each member in your class, whether it&#8217;s a field or a method. Each
access specifier controls the access for only that particular definition. This
is a distinct contrast to C++, in which the access specifier controls all the
definitions following it until another access specifier comes along.

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

<backtalk:display ID=TIJ3_CHAPTER5_I39>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One way or another, everything has some
kind of access specified for it. In the following sections, you&#8217;ll learn
all about the various types of access, starting with the default access.

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

<backtalk:display ID=TIJ3_CHAPTER5_I40>
</FONT><A NAME="_Toc312373840"></A><A NAME="_Toc375545296"></A><A NAME="_Toc481064595"></A><BR></P></DIV>
<A NAME="Heading197"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
&#8220;Friendly&#8221;</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What if you give no access specifier at
all, as in all the examples before this chapter? The default access has no
keyword, but it is commonly referred to as &#8220;friendly.&#8221; It means that
all the other classes in the current package have access to the friendly member,
but to all the classes outside of this package the member appears to be
<B>private</B>. Since a compilation unit&#8212;a file&#8212;can belong only to a
single package, all the classes within a single compilation unit are
automatically friendly with each other. Thus, friendly elements are also said to
have <A NAME="Index501"></A><A NAME="Index502"></A><I>package access</I>.

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

<backtalk:display ID=TIJ3_CHAPTER5_I41>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Friendly access allows you to group
related classes together in a package so that they can easily interact with each
other. When you put classes together in a package (thus granting mutual access
to their friendly members; e.g., making them &#8220;friends&#8221;) you
&#8220;own&#8221; the code in that package. It makes sense that only code you
own should have friendly access to other code you own. You could say that
friendly access gives a meaning or a reason for grouping classes together in a
package. In many languages the way you organize your definitions in files can be
willy-nilly, but in Java you&#8217;re compelled to
<A NAME="Index503"></A><A NAME="Index504"></A>organize them in a sensible
fashion. In addition, you&#8217;ll probably want to exclude classes that
shouldn&#8217;t have access to the classes being defined in the current package.

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

<backtalk:display ID=TIJ3_CHAPTER5_I42>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The class controls which code has access
to its members. There&#8217;s no magic way to &#8220;break in.&#8221; Code from
another package can&#8217;t show up and say, &#8220;Hi, I&#8217;m a friend of
<B>Bob</B>&#8217;s!&#8221; and expect to see the <B>protected</B>, friendly, and
<B>private</B> members of <B>Bob</B>. The only way to grant access to a member
is to: 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER5_I42' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER5_I43>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">Make the member
<B>public</B>. Then everybody, everywhere, can access it.

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

<backtalk:display ID=TIJ3_CHAPTER5_I44>
</FONT><LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">Make
the member friendly by leaving off any access specifier, and put the other
classes in the same package. Then the other classes can access the member.

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

<backtalk:display ID=TIJ3_CHAPTER5_I45>
</FONT><LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">As
you&#8217;ll see in Chapter 6, when inheritance is introduced, an inherited
class can access a <B>protected</B> member as well as a <B>public</B> member
(but not <B>private</B> members). It can access friendly members only if the two
classes are in the same package. But don&#8217;t worry about that now.

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

<backtalk:display ID=TIJ3_CHAPTER5_I46>
</FONT><LI><FONT FACE="Verdana">	</FONT><FONT FACE="Georgia">Provide
&#8220;accessor/mutator&#8221; methods (also known as &#8220;get/set&#8221;
methods) that read and change the value. This is the most civilized approach in
terms of OOP, and it is fundamental to JavaBeans, as you&#8217;ll see in Chapter
13.

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

<backtalk:display ID=TIJ3_CHAPTER5_I47>
</FONT><A NAME="_Ref351419800"></A><A NAME="_Toc375545297"></A><A NAME="_Toc481064596"></A></OL><A NAME="Heading198"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
public: interface access</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you use the <B>public</B> keyword,
it <A NAME="Index505"></A>means that the member declaration that immediately
follows <B>public</B> is available to everyone, in particular to the client
programmer who uses the library. Suppose you define a package <B>dessert</B>
containing the following compilation unit:

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

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

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c05:dessert:Cookie.java</font>
<font color=#009900>// Creates a library.</font>
<font color=#0000ff>package</font> c05.dessert;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Cookie {
  <font color=#0000ff>public</font> Cookie() { 
   System.out.println(<font color=#004488>"Cookie constructor"</font>); 
  }
  <font color=#0000ff>void</font> bite() { System.out.println(<font color=#004488>"bite"</font>); }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Remember, <B>Cookie.java</B> must reside
in a subdirectory called <B>dessert</B>, in a directory under <B>c05
</B>(indicating Chapter 5 of this book) that must be under one of the CLASSPATH
directories. Don&#8217;t make the mistake of thinking that Java will always look
at the current directory as one of the starting points for searching. If you
don&#8217;t have a &#8216;<B>.</B>&#8217; as one of the paths in your CLASSPATH,
Java won&#8217;t look there.

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

<backtalk:display ID=TIJ3_CHAPTER5_I49>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now if you create a program that uses
<B>Cookie</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c05:Dinner.java</font>
<font color=#009900>// Uses the library.</font>
<font color=#0000ff>import</font> c05.dessert.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Dinner {
  <font color=#0000ff>public</font> Dinner() {
   System.out.println(<font color=#004488>"Dinner constructor"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Cookie x = <font color=#0000ff>new</font> Cookie();
    <font color=#009900>//! x.bite(); // Can't access</font>
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

⌨️ 快捷键说明

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