📄 tij307.htm
字号:
<font color=#0000ff>public</font> <font color=#0000ff>void</font> pub1() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>public</font> <font color=#0000ff>void</font> pub2() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>public</font> <font color=#0000ff>void</font> pub3() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>private</font> <font color=#0000ff>void</font> priv1() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>private</font> <font color=#0000ff>void</font> priv2() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>private</font> <font color=#0000ff>void</font> priv3() { <font color=#009900>/* . . . */</font> }
<font color=#0000ff>private</font> <font color=#0000ff>int</font> i;
<font color=#009900>// . . .</font>
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This will make it only partially easier to read, because the interface and implementation are still mixed together. That is, you still see the source code—the implementation—because it’s right there in the class. In addition, the comment documentation supported by javadoc (described in Chapter 2) lessens the importance of code readability by the client programmer. Displaying the interface to the consumer of a class is really the job of the <a name="Index469"></a><a name="Index470"></a><i>class browser</i>, a tool whose job is to look at all the available classes and show you what you can do with them (i.e., what members are available) in a useful fashion. Class browsers have become an expected part of any good Java development tool. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_879" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc312373850"></a><a name="_Toc375545302"></a><a name="_Toc24775631"></a><a name="Heading4934"></a>Class
access<br></h2>
<p><a name="Index471"></a><a name="Index472"></a>In Java, the access specifiers can also be used to determine which classes <i>within</i> a library will be available to the users of that library. If you want a class to be available to a client programmer, you use the <b>public</b> keyword on the entire class<b> </b>definition. This controls whether the client programmer can even create an object of the class. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_880" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>To control the access of a class, the specifier must appear before the keyword <b>class</b>.<b> </b>Thus you can say:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>public</font> <font color=#0000ff>class</font> Widget {</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Now if the name of your library is <b>mylib</b>, any client programmer can access <b>Widget</b> by saying<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> mylib.Widget;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>or<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> mylib.*;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>However, there’s an extra set of constraints: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_881" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li>There can be only one <b>public</b> class per compilation unit (file). The
idea is that each compilation unit has a single public interface represented by
that <b>public</b> class. It can have as many supporting package-access classes
as you want. If you have more than one <b>public</b> class inside a compilation
unit, the compiler will give you an error message. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_882" title="Send BackTalk
Comment">Feedback</a></font></li>
<li>The name of the <b>public</b> class must exactly match the name of the file
containing the compilation unit, including capitalization. So for <b>Widget</b>,
the name of the file must be <b>Widget.java</b>, not <b>widget.java</b> or
<b>WIDGET.java</b>. Again, you’ll get a compile-time error if they
don’t agree. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_883" title="Send BackTalk
Comment">Feedback</a></font></li>
<li>It is possible, though not typical, to have a compilation unit with no
<b>public</b> class at all. In this case, you can name the file whatever you
like. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_884" title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>What if you’ve got a class inside <b>mylib</b> that you’re just using to accomplish the tasks performed by <b>Widget</b> or some other <b>public</b> class in <b>mylib</b>? 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 <b>mylib</b>. To accomplish this, you just leave the <b>public</b> keyword off the class, in which case it has package access. (That class can be used only within that package.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_885" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>When you create a package-access class, it still makes sense to make the fields of the class <b>private</b>—you should always make fields as private as possible—but it’s generally reasonable to give the methods the same access as the class (package access). Since a package-access class is usually used only within the package, you only need to make the methods of such a class <b>public</b> if you’re forced to, and in those cases, the compiler will tell you. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0463" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Note that a class cannot be <b>private </b>(that would make it accessible to no one but the class) or <b>protected</b>.<sup><a name="fnB30" href="#fn30">[30]</a></sup> So you have only two choices for class access: package access or <b>public</b>. If you don’t want anyone else to have access to that class, you can make all the constructors <b>private</b>, thereby preventing anyone but you, inside a <b>static</b> member of the class, from creating an object of that class. Here’s an example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0108" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c05:Lunch.java</font>
<font color=#009900>// Demonstrates class access specifiers. Make a class</font>
<font color=#009900>// effectively private with private constructors:</font>
<font color=#0000ff>class</font> Soup {
<font color=#0000ff>private</font> Soup() {}
<font color=#009900>// (1) Allow creation via static method:</font>
<font color=#0000ff>public</font> <font color=#0000ff>static</font> Soup makeSoup() {
<font color=#0000ff>return</font> <font color=#0000ff>new</font> Soup();
}
<font color=#009900>// (2) Create a static object and return a reference</font>
<font color=#009900>// upon request.(The "Singleton" pattern):</font>
<font color=#0000ff>private</font> <font color=#0000ff>static</font> Soup ps1 = <font color=#0000ff>new</font> Soup();
<font color=#0000ff>public</font> <font color=#0000ff>static</font> Soup access() {
<font color=#0000ff>return</font> ps1;
}
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>class</font> Sandwich { <font color=#009900>// Uses Lunch</font>
<font color=#0000ff>void</font> f() { <font color=#0000ff>new</font> Lunch(); }
}
<font color=#009900>// Only one public class allowed per file:</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Lunch {
<font color=#0000ff>void</font> test() {
<font color=#009900>// Can't do this! Private constructor:</font>
<font color=#009900>//! Soup priv1 = new Soup();</font>
Soup priv2 = Soup.makeSoup();
Sandwich f1 = <font color=#0000ff>new</font> Sandwich();
Soup.access().f();
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Up to now, most of the methods have been returning either <b>void</b> or a primitive type, so the definition:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE> <font color=#0000ff>public</font> <font color=#0000ff>static</font> Soup access() {
<font color=#0000ff>return</font> ps1;
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>might look a little confusing at first. The word before the method name (<b>access</b>) tells what the method returns. So far, this has most often been <b>void,</b> which means it returns nothing. But you can also return a reference to an object, which is what happens here. This method returns a reference to an object of class <b>Soup</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_886" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The <b>class Soup</b> shows how to prevent direct creation of a class by making all the constructors <b>private</b>. Remember that if you don’t explicitly create at least one constructor, the default constructor (a constructor with no arguments) will be created for you. By writing the default constructor, it won’t be created automatically. By making it <b>private</b>, no one can create an object of that class. But now how does anyone use this class? The preceding example shows two options. First, a <b>static</b> method is created that creates a new <b>Soup</b> and returns a reference to it. This could be useful if you want to do some extra operations on the <b>Soup</b> before returning it, or if you want to keep count of how many <b>Soup</b> objects to create (perhaps to restrict their population). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_887" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The second option uses what’s called a <a name="Index473"></a><a name="Index474"></a><a name="Index475"></a><i>design pattern</i>, which is covered in <i>Thinking in Patterns (with Java)</i> at <i>www.BruceEckel.com</i>. This particular pattern is called a “singleton” because it allows only a single object to ever be created. The object of class <a name="Index476"></a><b>Soup</b> is created as a <b>static</b> <b>private </b>member of <b>Soup</b>, so there’s one and only one, and you can’t get at it except through the <b>public</b> method <b>access( )</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap05_888" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As previously mentioned, if you don’t put an access specifier for class access, it defaults to package access. This means that an object of that class can be created by any other class in the package, but not outside the package. (Remember, all the files within the same directory that don’t have explicit <b>package </b>declarations are implicitly part of the default package for that directory.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -