📄 chap08.htm
字号:
System.out.println(RandVals.rfloat);
System.out.println(RandVals.rdouble);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The fields, of course, are not part of
the interface but instead are stored in the <B>static</B> storage area for that
interface.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I23'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I24>
</FONT><A NAME="_Toc481064648"></A><BR></P></DIV>
<A NAME="Heading259"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Nesting interfaces</H3></FONT>
<DIV ALIGN="LEFT"><P><A NAME="fnB39" HREF="#fn39">[39]</A><A NAME="Index777"></A><A NAME="Index778"></A><FONT FACE="Georgia">Interfaces
may be nested within classes and within other interfaces. This reveals a number
of very interesting features:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:NestingInterfaces.java</font>
<font color=#0000ff>class</font> A {
<font color=#0000ff>interface</font> B {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> BImp <font color=#0000ff>implements</font> B {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> BImp2 <font color=#0000ff>implements</font> B {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> C {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>class</font> CImp <font color=#0000ff>implements</font> C {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> CImp2 <font color=#0000ff>implements</font> C {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>interface</font> D {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> DImp <font color=#0000ff>implements</font> D {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> DImp2 <font color=#0000ff>implements</font> D {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> D getD() { <font color=#0000ff>return</font> <font color=#0000ff>new</font> DImp2(); }
<font color=#0000ff>private</font> D dRef;
<font color=#0000ff>public</font> <font color=#0000ff>void</font> receiveD(D d) {
dRef = d;
dRef.f();
}
}
<font color=#0000ff>interface</font> E {
<font color=#0000ff>interface</font> G {
<font color=#0000ff>void</font> f();
}
<font color=#009900>// Redundant "public":</font>
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> H {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>void</font> g();
<font color=#009900>// Cannot be private within an interface:</font>
<font color=#009900>//! private interface I {}</font>
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> NestingInterfaces {
<font color=#0000ff>public</font> <font color=#0000ff>class</font> BImp <font color=#0000ff>implements</font> A.B {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>class</font> CImp <font color=#0000ff>implements</font> A.C {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#009900>// Cannot implement a private interface except</font>
<font color=#009900>// within that interface's defining class:</font>
<font color=#009900>//! class DImp implements A.D {</font>
<font color=#009900>//! public void f() {}</font>
<font color=#009900>//! }</font>
<font color=#0000ff>class</font> EImp <font color=#0000ff>implements</font> E {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> g() {}
}
<font color=#0000ff>class</font> EGImp <font color=#0000ff>implements</font> E.G {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>class</font> EImp2 <font color=#0000ff>implements</font> E {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> g() {}
<font color=#0000ff>class</font> EG <font color=#0000ff>implements</font> E.G {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
A a = <font color=#0000ff>new</font> A();
<font color=#009900>// Can't access A.D:</font>
<font color=#009900>//! A.D ad = a.getD();</font>
<font color=#009900>// Doesn't return anything but A.D:</font>
<font color=#009900>//! A.DImp2 di2 = a.getD();</font>
<font color=#009900>// Cannot access a member of the interface:</font>
<font color=#009900>//! a.getD().f();</font>
<font color=#009900>// Only another A can do anything with getD():</font>
A a2 = <font color=#0000ff>new</font> A();
a2.receiveD(a.getD());
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The syntax for nesting an interface
within a class is reasonably obvious, and just like non-nested interfaces these
can have <B>public</B> or “friendly” visibility. You can also see
that both <B>public</B> and “friendly” nested interfaces can be
implemented as a <B>public</B>, “friendly,” and <B>private</B>
nested classes.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I24'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a new twist,
<A NAME="Index779"></A><A NAME="Index780"></A>interfaces can also be
<B>private</B> as seen in <B>A.D</B> (the same qualification syntax is used for
nested interfaces as for nested classes). What good is a <B>private</B> nested
interface? You might guess that it can only be implemented as a <B>private</B>
nested class as in <B>DImp</B>, but <B>A.DImp2</B> shows that it can also be
implemented as a <B>public</B> class. However, <B>A.DImp2</B> can only be used
as itself. You are not allowed to mention the fact that it implements the
<B>private</B> interface, so implementing a <B>private</B> interface is a way to
force the definition of the methods in that interface without adding any type
information (that is, without allowing any upcasting).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I25'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The method <B>getD( )</B> produces a
further quandary concerning the <B>private</B> interface: it’s a
<B>public</B> method that returns a reference to a <B>private</B> interface.
What can you do with the return value of this method? In <B>main( )</B>,
you can see several attempts to use the return value, all of which fail. The
only thing that works is if the return value is handed to an object that has
permission to use it—in this case, another <B>A</B>, via the
<B><STRIKE>received</STRIKE><U>receiveD</U>( )</B> method.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I26'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Interface <B>E</B> shows that interfaces
can be nested within each other. However, the rules about interfaces—in
particular, that all interface elements must be <B>public</B>—are strictly
enforced here, so an interface nested within another interface is automatically
<B>public</B> and cannot be made <B>private</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I27'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I28>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>NestingInterfaces</B> shows the
various ways that nested interfaces can be implemented. In particular, notice
that when you implement an interface, you are not required to implement any
interfaces nested within. Also, <B>private</B> interfaces cannot be implemented
outside of their defining classes.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I28'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I29>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Initially, these features may seem like
they are added strictly for syntactic consistency, but I generally find that
once you know about a feature, you often discover places where it is useful.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I29'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I30>
</FONT><A NAME="_Toc481064649"></A><BR></P></DIV>
<A NAME="Heading260"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Inner classes</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It’s possible to place a class
definition within another class definition. This is called an <I>inner
class</I>. The <A NAME="Index781"></A><A NAME="Index782"></A>inner class is a
valuable feature because it allows you to group classes that logically belong
together and to control the visibility of one within the other. However,
it’s important to understand that inner classes are distinctly different
from composition.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I30'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I31>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Often, while you’re learning about
them, the need for inner classes isn’t immediately obvious. At the end of
this section, after all of the syntax and semantics of inner classes have been
described, you’ll find examples that should make clear the benefits of
inner classes.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I31'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I32>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You create an inner class just as
you’d expect—by placing the class definition inside a surrounding
class:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I32'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I33>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Parcel1.java</font>
<font color=#009900>// Creating inner classes.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Parcel1 {
<font color=#0000ff>class</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>class</font> Destination {
<font color=#0000ff>private</font> String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() { <font color=#0000ff>return</font> label; }
}
<font color=#009900>// Using inner classes looks just like</font>
<font color=#009900>// using any other class, within Parcel1:</font>
<font color=#0000ff>public</font> <font color=#0000ff>void</font> ship(String dest) {
Contents c = <font color=#0000ff>new</font> Contents();
Destination d = <font color=#0000ff>new</font> Destination(dest);
System.out.println(d.readLabel());
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Parcel1 p = <font color=#0000ff>new</font> Parcel1();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -