📄 tij0060.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="tij0059.html">Prev</a> | <a href="tij0061.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Class
access
<P><A NAME="Index411"></A><A NAME="Index412"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Java, the access specifiers can also be used to determine which classes
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>within</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a library will be available to the users of that library. If you want a class
to be available to a client programmer, you place the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword somewhere before the opening brace of the class body. This controls
whether the client programmer can even create an object of the class.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
control the access of a class, the specifier must appear before the keyword
</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"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thus
you can say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">public
class Widget {
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">That
is, if the name of your library is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mylib</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
any client programmer can access
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Widget</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by saying
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
mylib.Widget;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
mylib.*;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">However,
there’s an extra pair of constraints:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> There
can be 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 per compilation unit (file). The idea is that each compilation unit has a
single public interface represented by that public class. It can have as many
supporting “friendly” classes as you want. If you have more than 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 inside a compilation unit, the compiler will give you an error message.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> The
name of 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 must exactly match the name of the file containing the compilation unit,
including capitalization. So for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Widget</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the name of the file must be
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Widget.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
not
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>widget.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WIDGET.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Again, you’ll get a compile-time error if they don’t agree.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> It
is possible, though not typical, to have a compilation unit with no public
class at all. In this case, you can name the file whatever you like.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
if you’ve got a class inside
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mylib</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that you’re
<a name="just"></a>just
using to accomplish the tasks performed by
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Widget</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or some other
</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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mylib</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">?
You don’t want to go to the bother of creating documentation for the
client programmer, and you think that sometime later you might want to
completely change things and rip out your class altogether, substituting a
different one. To give you this flexibility, you need to ensure that no client
programmers become dependent on your particular implementation details hidden
inside
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mylib</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
To accomplish this, you just leave the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword off the class, in which case it becomes friendly. (That class can be
used only within that package.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that a class cannot be
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(that
would make it accessible to no one but the class), or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>protected</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><A NAME="fnB26" HREF="#fn26">[26]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
So you have only two choices for class access: “friendly” or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you don’t want anyone else to have access to that class, you can make
all the constructors
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
thereby preventing anyone but you, inside 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, from creating an object of that class.
</FONT><A NAME="fnB27" HREF="#fn27">[27]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
Here’s an example:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Lunch.java</font>
<font color="#009900">// Demonstrates class access specifiers.</font>
<font color="#009900">// Make a class effectively private</font>
<font color="#009900">// with private constructors:</font>
<font color="#0000ff">class</font> Soup {
<font color="#0000ff">private</font> Soup() {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -