📄 tij0060.html
字号:
<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</font>
<font color="#009900">// return a reference upon request.</font>
<font color="#009900">// (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">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Up
to now, most of the methods have been returning either
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or a primitive type so the definition:
</FONT><P></DIV>
<font color="#990000"><PRE> <font color="#0000ff">public</font> <font color="#0000ff">static</font> Soup access() {
<font color="#0000ff">return</font> ps1;
} </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">might
look a little confusing at first. The word before the method name (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>access</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
tells what the method returns. So far this has most often been
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which means it returns nothing. But you can also return a handle to an object,
which is what happens here. This method returns a handle to an object of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><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>class
Soup
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
shows how to prevent direct creation of a class by making 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">.
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
no one can create an object of that class. But now how does anyone use this
class? The above example shows two options. First, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is created that creates a new
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and returns a handle to it. This could be useful if you want to do some extra
operations on the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
before returning it, or if you want to keep count of how many
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects to create (perhaps to restrict their population).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second option uses what’s called a <A NAME="Index413"></A><A NAME="Index414"></A><A NAME="Index415"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>design
pattern
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which will be discussed later in this book. This particular pattern is called a
“<A NAME="Index416"></A>singleton”
because it allows only a single object to ever be created. The object of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created as a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">member
of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Soup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so there’s one and only one, and you can’t get at it except through
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>access( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
previously mentioned, if you don’t put an access specifier for class
access it defaults to “friendly.” 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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">declarations
are implicitly part of the default package for that directory.) However, if 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 that class is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the client programmer can still access that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member even though they cannot create an object of that class.
</FONT><a name="_Toc375545303"></a><a name="_Toc408018506"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn26" HREF="#fnB26">[26]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
Actually, a Java 1.1
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><I>inner
class
</I></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
can be private or protected, but that’s a special case. These will be
introduced in Chapter 7.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="fn27" HREF="#fnB27">[27]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
You can also do it by inheriting (Chapter 6) from that class.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0059.html">Prev</a> | <a href="tij0061.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -