📄 chap08.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:10
Translation Platform:Win32
Number of Output files:23
This File:Chap08.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>8: Interfaces & Inner Classes</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README.txt">Viewing Hints</a> ]
[ <a href="RevHist.htm">Revision History</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> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 2nd edition, Revision 12</FONT></H2>
<H3><FONT FACE="Verdana">©2000 by Bruce Eckel</FONT></H3>
<FONT FACE="Verdana" size = "-1">
[ <a href="Chap07.htm">Previous Chapter</a> ]
[ <a href="SimpCont.htm">Short TOC</a> ]
[ <a href="Contents.htm">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Chap09.htm">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><A NAME="Chapter_8"></A><A NAME="_Toc375545346"></A><A NAME="_Toc477690728"></A><A NAME="_Toc481064642"></A><A NAME="Heading252"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
8: Interfaces & Inner Classes</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER8_I0>
Interfaces
and inner classes provide more sophisticated ways to organize and control the
objects in your system.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I0'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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—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’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++’s “multiple inheritance,” by creating a
class that can be upcast to more than one base type.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I1'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At first, inner classes look like a
simple code-hiding mechanism: you place classes inside other classes.
You’ll learn, however, that the inner class does more than that—it
knows about and can communicate with the surrounding class—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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I2'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I3>
</FONT><A NAME="_Toc481064643"></A><BR></P></DIV>
<A NAME="Heading253"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Interfaces</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The
<A NAME="Index752"></A><B>interface</B> keyword takes the <B>abstract
</B>concept one step further. You could think of it as a “pure”
<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="Index753"></A><B>static</B> and <A NAME="Index754"></A><B>final</B>. An
<B>interface</B> provides only a form, but no
<A NAME="Index755"></A>implementation.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I3'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An <B>interface</B> says: “This is
what all classes that <I>implement</I> this particular interface will look
like.” Thus, any code that uses a particular <B>interface</B> knows what
methods might be called for that <B>interface</B>, and that’s all. So the
<B>interface</B> is used to establish a “protocol” between classes.
(Some object-oriented programming languages have a keyword called
<A NAME="Index756"></A><A NAME="Index757"></A><I>protocol</I> to do the same
thing.)
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I4'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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="Index758"></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 “<A NAME="Index759"></A>friendly”
status so that it is only usable within the same package.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I5'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I6>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To make a class that conforms to a
particular <B>interface</B> (or group of <B>interface</B>s) use the
<A NAME="Index760"></A><B>implements</B> keyword. You’re saying “The
<B>interface</B> is what it looks like but now I’m going to say how it
<I>works</I>.” Other than that, it looks like inheritance. The diagram for
the instrument example shows this:</FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><IMG SRC="TIJ223.gif"></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Once you’ve implemented an
<B>interface</B>, that implementation becomes an ordinary class that can be
extended in the regular way.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I6'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I7>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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’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 “friendly,” and
you’d be reducing the accessibility of a method during inheritance, which
is not allowed by the Java compiler.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER8_I7'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER8_I8>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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’re automatically <B>public</B>
anyway:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:music5:Music5.java</font>
<font color=#009900>// Interfaces.</font>
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>interface</font> Instrument {
<font color=#009900>// Compile-time constant:</font>
<font color=#0000ff>int</font> i = 5; <font color=#009900>// static & final</font>
<font color=#009900>// Cannot have method definitions:</font>
<font color=#0000ff>void</font> play(); <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() {
System.out.println(<font color=#004488>"Wind.play()"</font>);
}
<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() {
System.out.println(<font color=#004488>"Percussion.play()"</font>);
}
<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() {
System.out.println(<font color=#004488>"Stringed.play()"</font>);
}
<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() {
System.out.println(<font color=#004488>"Brass.play()"</font>);
}
<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() {
System.out.println(<font color=#004488>"Woodwind.play()"</font>);
}
<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=#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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -