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

📄 tij310.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!--
This document was converted from RTF source: 
By r2net 5.8 r2netcmd Windows 
See http://www.logictran.com
-->
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thinking in Java, 3rd ed. Revision 4.0: 8: Interfaces &amp; Inner Classes</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"></head>

<body >
   <CENTER>     <a href="http://www.MindView.net">     <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>     <Font FACE="Verdana, Tahoma, Arial, Helvetica, Sans">     <h2>Thinking in Java, 3<sup>rd</sup> ed. Revision 4.0</h2>     <FONT size = "-1"><br>     [ <a href="README.txt">Viewing Hints</a> ]     [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]     [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>     [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]     [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]     [ <a href="http://www.mindview.net/Services">Consulting</a> ] <br><br>     </FONT></FONT>   </CENTER> 
<font face="Georgia"><div align="CENTER"><a href="TIJ309.htm" target="RightFrame"><img src="./prev.gif" alt="Previous " border="0"></a>
<a href="TIJ311.htm" target="RightFrame"><img src="./next.gif" alt="Next " border="0"></a>

<a href="TIJ3_t.htm"><img src="./first.gif" alt="Title Page " border="0"></a>
<a href="TIJ3_i.htm"><img src="./index.gif" alt="Index " border="0"></a>
<a href="TIJ3_c.htm"><img src="./contents.gif" alt="Contents " border="0"></a>
</div>
<hr>

<h1>
<a name="_Toc375545346"></a><a name="_Toc24272647"></a><a name="_Toc24775673"></a><a name="Heading6797"></a>8:
Interfaces &amp; Inner Classes</h1>
<p class="Intro">Interfaces and inner classes provide more sophisticated ways to organize and control the objects in your system.<br></p>
<p>C++, for example, does not contain such mechanisms, although the clever programmer may simulate them. The fact that they exist in Java indicates that they were considered important enough to provide direct support through language keywords. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1108" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In Chapter 7 you learned about the <b>abstract</b> keyword, which allows you to create one or more methods in a class that have no definitions&#151;you provide part of the interface without providing a corresponding implementation, which is created by inheritors. The <b>interface</b> keyword produces a completely abstract class, one that provides no implementation at all. You&#146;ll learn that the <b>interface </b>is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++&#146;s &#147;multiple inheritance&#148; by creating a class that can be upcast to more than one base type. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1109" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>At first, inner classes look like a simple code-hiding mechanism: you place classes inside other classes. You&#146;ll learn, however, that the inner class does more than that&#151;it knows about and can communicate with the surrounding class&#151;and that the kind of code you can write with inner classes is more elegant and clear, although it is a new concept to most. It takes some time to become comfortable with design using inner classes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1110" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775674"></a><a name="Heading6802"></a>Interfaces</h2>
<p>The <a name="Index687"></a><b>interface</b> keyword takes the <b>abstract </b>concept one step further. You could think of it as a &#147;pure&#148; <b>abstract </b>class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An <b>interface</b> can also contain fields, but these are implicitly <a name="Index688"></a><b>static</b> and <a name="Index689"></a><b>final</b>. An <b>interface</b> provides only a form, but no implementation. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1111" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index690"></a>An <b>interface</b> says, &#147;This is what all classes that <i>implement</i> this particular interface will look like.&#148; Thus, any code that uses a particular <b>interface</b> knows what methods might be called for that <b>interface</b>, and that&#146;s all. So the <b>interface</b> is used to establish a &#147;protocol&#148; between classes. (Some object-oriented programming languages have a keyword called <a name="Index691"></a><a name="Index692"></a><i>protocol</i> to do the same thing.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1112" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>To create an <b>interface</b>, use the <b>interface</b> keyword instead of the <b>class</b> keyword. Like a class, you can add the <a name="Index693"></a><b>public</b> keyword before the <b>interface </b>keyword (but only if that <b>interface</b> is defined in a file of the same name) or leave it off to give package access, so that it is only usable within the same package. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1113" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>To make a class that conforms to a particular <b>interface</b> (or group of <b>interface</b>s), use the <a name="Index694"></a><b>implements</b> keyword, which says, &#147;The <b>interface</b> is what it looks like, but now I&#146;m going to say how it <i>works</i>.&#148; Other than that, it looks like inheritance. The diagram for the instrument example shows this:<br></p>
<p align="center"><img src="TIJ322.png" 	alt="TIJ322.png" border="0" ><br></p>
<p>You can see from the <b>Woodwind </b>and <b>Brass </b>classes that once you&#146;ve implemented an <b>interface</b>, that implementation becomes an ordinary class that can be extended in the regular way. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1114" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can choose to explicitly declare the method declarations in an <b>interface</b> as <b>public</b>, but they are <b>public</b> even if you don&#146;t say it. So when you <b>implement</b> an <b>interface</b>, the methods from the <b>interface</b> must be defined as <b>public</b>. Otherwise, they would default to package access, and you&#146;d be reducing the accessibility of a method during inheritance, which is not allowed by the Java compiler. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1115" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can see this in the modified version of the <b>Instrument</b> example. Note that every method in the <b>interface</b> is strictly a declaration, which is the only thing the compiler allows. In addition, none of the methods in <b>Instrument</b> are declared as <b>public</b>, but they&#146;re automatically <b>public</b> anyway:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:music5:Music5.java</font>
<font color=#009900>// Interfaces.</font>
<font color=#0000ff>package</font> c08.music5;
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>import</font> c07.music.Note;

<font color=#0000ff>interface</font> Instrument {
  <font color=#009900>// Compile-time constant:</font>
  <font color=#0000ff>int</font> I = 5; <font color=#009900>// static &amp; final</font>
  <font color=#009900>// Cannot have method definitions:</font>
  <font color=#0000ff>void</font> play(Note n); <font color=#009900>// Automatically public</font>
  String what();
  <font color=#0000ff>void</font> adjust();
}

<font color=#0000ff>class</font> Wind <font color=#0000ff>implements</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Wind.play() "</font> + n);
  }
  <font color=#0000ff>public</font> String what() { <font color=#0000ff>return</font> <font color=#004488>"Wind"</font>; }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> adjust() {}
}

<font color=#0000ff>class</font> Percussion <font color=#0000ff>implements</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Percussion.play() "</font> + n);
  }
  <font color=#0000ff>public</font> String what() { <font color=#0000ff>return</font> <font color=#004488>"Percussion"</font>; }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> adjust() {}
}

<font color=#0000ff>class</font> Stringed <font color=#0000ff>implements</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Stringed.play() "</font> + n);
  }
  <font color=#0000ff>public</font> String what() { <font color=#0000ff>return</font> <font color=#004488>"Stringed"</font>; }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> adjust() {}
}

<font color=#0000ff>class</font> Brass <font color=#0000ff>extends</font> Wind {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Brass.play() "</font> + n);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> adjust() {
    System.out.println(<font color=#004488>"Brass.adjust()"</font>);
  }
}

<font color=#0000ff>class</font> Woodwind <font color=#0000ff>extends</font> Wind {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Woodwind.play() "</font> + n);
  }
  <font color=#0000ff>public</font> String what() { <font color=#0000ff>return</font> <font color=#004488>"Woodwind"</font>; }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Music5 {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#009900>// Doesn't care about type, so new types</font>
  <font color=#009900>// added to the system still work right:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> tune(Instrument i) {
    <font color=#009900>// ...</font>
    i.play(Note.MIDDLE_C);
  }
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> tuneAll(Instrument[] e) {
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; e.length; i++)
      tune(e[i]);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#009900>// Upcasting during addition to the array:</font>
    Instrument[] orchestra = {
      <font color=#0000ff>new</font> Wind(),
      <font color=#0000ff>new</font> Percussion(),
      <font color=#0000ff>new</font> Stringed(),
      <font color=#0000ff>new</font> Brass(),
      <font color=#0000ff>new</font> Woodwind()
    };
    tuneAll(orchestra);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Wind.play() Middle C"</font>,
      <font color=#004488>"Percussion.play() Middle C"</font>,
      <font color=#004488>"Stringed.play() Middle C"</font>,
      <font color=#004488>"Brass.play() Middle C"</font>,
      <font color=#004488>"Woodwind.play() Middle C"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The rest of the code works the same. It doesn&#146;t matter if you are upcasting to a &#147;regular&#148; class called <a name="Index695"></a><b>Instrument</b>, an <b>abstract</b> class called <b>Instrument</b>, or to an <a name="Index696"></a><b>interface</b> called <b>Instrument</b>. The behavior is the same. In fact, you can see in the <b>tune(&#160;)</b> method that there isn&#146;t any evidence about whether <b>Instrument</b> is a &#147;regular&#148; class, an <b>abstract</b> class, or an <b>interface</b>. This is the intent: Each approach gives the programmer different control over the way objects are created and used. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1116" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545336"></a><a name="_Toc24775675"></a><a name="Heading6899"></a>&#147;Multiple
inheritance&#148; in Java</h3>
<p>The <b>interface</b> isn&#146;t simply a &#147;more pure&#148; form of <b>abstract</b> class. It has a higher purpose than that. Because an <b>interface</b> has no implementation at all&#151;that is, there is no storage associated with an <b>interface&#151;</b>there&#146;s nothing to prevent many <b>interface</b>s from being combined. This is valuable because there are times when you need to say &#147;An <b>x</b> is an <b>a</b> <i>and</i> a <b>b</b> <i>and</i> a <b>c</b>.&#148; In C++, this act of combining multiple class interfaces is called <a name="Index697"></a><a name="Index698"></a><i>multiple inheritance</i>, and it carries some rather sticky baggage because each class can have an implementation. In Java, you can perform the same act, but only one of the classes can have an implementation, so the problems seen in C++ do not occur with Java when combining multiple interfaces:<br></p>
<p align="center"><img src="TIJ323.png" 	alt="TIJ323.png" border="0" ><br></p>
<p>In a derived class, you aren&#146;t forced to have a base class that is either an <b>abstract</b> or &#147;concrete&#148; (one with no <b>abstract</b> methods). If you <i>do</i> inherit from a non-<b>interface</b>,<b> </b>you can inherit from only one. All the rest of the base elements must be <b>interface</b>s. You place all the interface names after the <b>implements </b>keyword and separate them with commas. You can have as many <b>interface</b>s as you want; each one becomes an independent type that you can upcast to. The following example shows a concrete class combined with several <b>interface</b>s to produce a new class: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1117" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Adventure.java</font>
<font color=#009900>// Multiple interfaces.</font>

<font color=#0000ff>interface</font> CanFight {
  <font color=#0000ff>void</font> fight();
}

⌨️ 快捷键说明

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