📄 tij0081.html
字号:
<font color="#0000ff">interface</font> Selector {
<font color="#0000ff">boolean</font> end();
Object current();
<font color="#0000ff">void</font> next();
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Sequence {
<font color="#0000ff">private</font> Object[] o;
<font color="#0000ff">private</font> <font color="#0000ff">int</font> next = 0;
<font color="#0000ff">public</font> Sequence(<font color="#0000ff">int</font> size) {
o = <font color="#0000ff">new</font> Object[size];
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> add(Object x) {
<font color="#0000ff">if</font>(next < o.length) {
o[next] = x;
next++;
}
}
<font color="#0000ff">private</font> <font color="#0000ff">class</font> SSelector <font color="#0000ff">implements</font> Selector {
<font color="#0000ff">int</font> i = 0;
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> end() {
<font color="#0000ff">return</font> i == o.length;
}
<font color="#0000ff">public</font> Object current() {
<font color="#0000ff">return</font> o[i];
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> next() {
<font color="#0000ff">if</font>(i < o.length) i++;
}
}
<font color="#0000ff">public</font> Selector getSelector() {
<font color="#0000ff">return</font> <font color="#0000ff">new</font> SSelector();
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Sequence s = <font color="#0000ff">new</font> Sequence(10);
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 10; i++)
s.add(Integer.toString(i));
Selector sl = s.getSelector();
<font color="#0000ff">while</font>(!sl.end()) {
System.out.println((String)sl.current());
sl.next();
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><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>Sequence</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is simply a fixed-sized array of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with a class wrapped around it. You call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to add a new
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to the end of the sequence (if there’s room left). To fetch each of the
objects in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Sequence</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
there’s an interface called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Selector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which allows you to see if you’re at the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>end( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
to look at the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>current( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and to move to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>next( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Sequence</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Because
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Selector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
many other classes can implement the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in their own ways, and many methods can take the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as an argument, in order to create generic code.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here,
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SSelector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a private class that provides
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Selector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
functionality. In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you can see the creation of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Sequence</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
followed by the addition of a number of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. Then, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Selector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is produced with a call to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getSelector( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and this is used to move through the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Sequence</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and select each item.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
first, the creation of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SSelector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
looks like just another inner class. But examine it closely. Note that each of
the methods
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>end( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>current( ),</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>next( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
refer to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>o</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is a handle that isn’t part of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SSelector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but is instead a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">field
in the enclosing class. However, the inner class can access methods and fields
from the enclosing class as if they owned them. This turns out to be very
convenient, as you can see in the above example.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
an inner class has access to the members of the enclosing class. How can this
happen? The <A NAME="Index640"></A>inner
class must keep a reference to the particular object of the enclosing class
that was responsible for creating it. Then when you refer to a member of the
enclosing class, that (hidden) reference is used to select that member.
Fortunately, the compiler takes care of all these details for you, but you can
also understand now that an object of an inner class can be created only in
association with an object of the enclosing class. The process of construction
requires the initialization of the handle to the object of the enclosing class,
and the compiler will complain if it cannot access the handle. Most of the time
this occurs without any intervention on the part of the programmer.
</FONT><a name="_Toc408018547"></a><P></DIV>
<A NAME="Heading225"></A><H3 ALIGN=LEFT>
static
inner classes
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
understand the meaning of <A NAME="Index641"></A><A NAME="Index642"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
when applied to inner classes, you must remember that the object of the inner
class implicitly keeps a handle to the object of the enclosing class that
created it. This is not true, however, when you say an inner class is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">inner
class means:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> You
don’t need an outer-class object in order to create an object of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inner class.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> You
can’t access an outer-class object from an object of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inner class.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are some restrictions:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members can be at only the outer level of a class, so inner classes cannot have
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inner classes.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index643"></A><A NAME="Index644"></A><A NAME="Index645"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you don’t need to create an object of the outer class in order to create
an object of the inner class, you can make everything
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
For this to work, you must also make the inner classes
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Parcel10.java</font>
<font color="#009900">// Static inner classes</font>
<font color="#0000ff">package</font> c07.parcel10;
<font color="#0000ff">abstract</font> <font color="#0000ff">class</font> Contents {
<font color="#0000ff">abstract</font> <font color="#0000ff">public</font> <font color="#0000ff">int</font> value();
}
<font color="#0000ff">interface</font> Destination {
String readLabel();
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Parcel10 {
<font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">class</font> PContents
<font color="#0000ff">extends</font> Contents {
<font color="#0000ff">private</font> <font color="#0000ff">int</font> i = 11;
<font color="#0000ff">public</font> <font color="#0000ff">int</font> value() { <font color="#0000ff">return</font> i; }
}
<font color="#0000ff">protected</font> <font color="#0000ff">static</font> <font color="#0000ff">class</font> PDestination
<font color="#0000ff">implements</font> Destination {
<font color="#0000ff">private</font> String label;
<font color="#0000ff">private</font> PDestination(String whereTo) {
label = whereTo;
}
<font color="#0000ff">public</font> String readLabel() { <font color="#0000ff">return</font> label; }
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Destination dest(String s) {
<font color="#0000ff">return</font> <font color="#0000ff">new</font> PDestination(s);
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Contents cont() {
<font color="#0000ff">return</font> <font color="#0000ff">new</font> PContents();
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Contents c = cont();
Destination d = dest("Tanzania");
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
no object of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Parcel10</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is necessary; instead you use the normal syntax for selecting 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 to call the methods that return handles to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Contents</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Destination</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">Normally
you can't put any code inside an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -