⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij310.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
  <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><p><br></p>
<p>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 package-access visibility. You can also see that both <b>public</b> and package-access nested interfaces can be implemented as <b>public</b>, package-access, and <b>private</b> nested classes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1132" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As a new twist, interfaces can also be <a name="Index713"></a><a name="Index714"></a><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> inner 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). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1133" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The method <b>getD(&#160;)</b> produces a further quandary concerning the <b>private</b> interface: It&#146;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(&#160;)</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&#151;in this case, another <b>A</b>, via the <b>receiveD(&#160;)</b> method. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1134" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Interface <b>E</b> shows that interfaces can be nested within each other. However, the rules about interfaces&#151;in particular, that all interface elements must be <b>public</b>&#151;are strictly enforced here, so an interface nested within another interface is automatically <b>public</b> and cannot be made <b>private</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1135" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1136" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1137" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775680"></a><a name="Heading7235"></a>Inner classes</h2>
<p>It&#146;s possible to place a class definition within another class definition. This is called an <i>inner class</i>. The 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&#146;s important to understand that inner classes are distinctly different from composition. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1138" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index715"></a><a name="Index716"></a>While you&#146;re learning about them, the need for inner classes isn&#146;t always obvious. At the end of this section, after all of the syntax and semantics of inner classes have been described, you&#146;ll find examples that should begin to make clear the benefits of inner classes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1139" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You create an inner class just as you&#146;d expect&#151;by placing the class definition inside a surrounding class: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1140" title="Send BackTalk Comment">Feedback</a></font><br></p>

<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();
    p.ship(<font color=#004488>"Tanzania"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The inner classes, when used inside <b>ship(&#160;)</b>, look just like the use of any other classes. Here, the only practical difference is that the names are nested within <b>Parcel1</b>. You&#146;ll see in a while that this isn&#146;t the only difference. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1141" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>More typically, an outer class will have a method that returns a reference to an inner class, like this:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Parcel2.java</font>
<font color=#009900>// Returning a reference to an inner class.</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Parcel2 {
  <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=#0000ff>public</font> Destination to(String s) {
    <font color=#0000ff>return</font> <font color=#0000ff>new</font> Destination(s);
  }
  <font color=#0000ff>public</font> Contents cont() {
    <font color=#0000ff>return</font> <font color=#0000ff>new</font> Contents();
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> ship(String dest) {
    Contents c = cont();
    Destination d = to(dest);
    System.out.println(d.readLabel());
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Parcel2 p = <font color=#0000ff>new</font> Parcel2();
    p.ship(<font color=#004488>"Tanzania"</font>);
    Parcel2 q = <font color=#0000ff>new</font> Parcel2();
    <font color=#009900>// Defining references to inner classes:</font>
    Parcel2.Contents c = q.cont();
    Parcel2.Destination d = q.to(<font color=#004488>"Borneo"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>If you want to make an object of the inner class anywhere except from within a non-<b>static</b> method of the outer class, you must specify the type of that object as <i>OuterClassName.InnerClassName</i>, as seen in <b>main(&#160;)</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1142" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775681"></a><a name="Heading7306"></a>Inner classes and
upcasting<br></h3>
<p><a name="Index717"></a><a name="Index718"></a><a name="Index719"></a>So far, inner classes don&#146;t seem that dramatic. After all, if it&#146;s hiding you&#146;re after, Java already has a perfectly good hiding mechanism&#151;just give the class package access (visible only within a package) rather than creating it as an inner class. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1143" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index720"></a><a name="Index721"></a><a name="Index722"></a>However, inner classes really come into their own when you start upcasting to a base class, and in particular to an <b>interface</b>. (The effect of producing an interface reference from an object that implements it is essentially the same as upcasting to a base class.) That&#146;s because the inner class&#151;the implementation of the <b>interface</b>&#151;can then be completely unseen and unavailable to anyone, which is convenient for hiding the implementation. All you get back is a reference to the base class or the <b>interface</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1144" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>First, the common interfaces will be defined in their own files so they can be used in all the examples:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Destination.java</font>
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> Destination {
  String readLabel();
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Contents.java</font>
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> Contents {
  <font color=#0000ff>int</font> value();
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Now <b>Contents</b> and <b>Destination</b> represent interfaces available to the client programmer. (The <b>interface</b>, remember, automatically makes all of its members <b>public</b>.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1145" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>When you get back a reference to the base class or the <b>interface</b>, it&#146;s possible that you can&#146;t even find out the exact type, as shown here:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:TestParcel.java</font>
<font color=#009900>// Returning a reference to an inner class.</font>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -