📄 082-085.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Java Programming: The Basics</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=2//-->
<!--PAGES=082-085//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="078-082.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="085-088.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Interfaces</B></FONT></P>
<P>An interface is like an abstract class that has only abstract methods and constant fields. The interface can hold no method implementations and is defined like a class except that it uses the keyword <I>interface</I> rather than <I>class</I>. What follows is the MBNF for the <I>interface</I> declaration:</P>
<!-- CODE SNIP //-->
<PRE>
interfaceDeclaration ->
< modifier > “interface” identifier [ “extends” interfaceName < “,”
interfaceName > ] “{“ < fieldDeclaration > “}” .
fieldDeclaration ->
( [docComment] ( methodDeclaration | constructorDeclaration |
variableDeclaration ) ) | staticInitializer | “;” .
interfaceName ->
identifier | ( packageName “.” identifier ) .
</PRE>
<!-- END CODE SNIP //-->
<P>Note that an <I>interface</I> declaration can extend multiple interfaces. The interface can serve as another reference type but can never be instanced. Thus, classes that implement an interface can always be cast back to the <I>interface</I> type.</P>
<P>Class identifiers and interface identifiers are always global to the package in which they reside. Therefore, classes and interfaces in the same package must have different identifiers. It is redundant to declare an interface as abstract, because interfaces are implicitly abstract. It is redundant to declare the field declarations as public, static, and final, because they are implicitly public, static, and final. All fields must be initialized. There is no primordial interface analogous to the <I>Object</I> class. It is a compile-time error to have ambiguous inherited fields in an interface. For example:</P>
<!-- CODE //-->
<PRE>
public interface real_dumb {
double PI = 4;
}
public interface dum_constants {
double PI = 3;
}
public interface mixed_up_constants extends dum_constants,
real_dumb {double foo=PI;}
Error : Reference to PI is ambiguous. It is defined in
interface real_dumb and interface dum_constants.
constants.java line 17 {double foo=PI;}
</PRE>
<!-- END CODE //-->
<P>The following are some correct uses of interfaces. The first example shows how interfaces can be used to group constants:
</P>
<!-- CODE SNIP //-->
<PRE>
public interface constants {
double Pi_on_180 = Math.PI / 180;
double PI = Math.PI;
double Pi_on_2 = Math.PI/2;
double Pi_on_4 = Math.PI/4;
}
</PRE>
<!-- END CODE SNIP //-->
<P>In the following example, a large array of symbols is being stored:
</P>
<!-- CODE //-->
<PRE>
public interface CplusplusText {
public static String cplusplusReservedWords[] = {
“asm”,
“auto”,
“break”,
“case”,
“catch”,
“char”,
“class”,
“const”,
“continue”,
“default”,
“delete”,
“do”,
“double”,
“else”,
“enum”,
“extern”,
“float”,
“friend”,
“for”,
“goto”,
“if”,
“inline”,
“int”,
“long”,
“new”,
“operator”,
“private”,
“protected”,
“public”,
“register”,
“return”,
“short”,
“signed”,
“sizeof”,
“static”,
“struct”,
“switch”,
“this”,
“throw”,
“try”,
“typedef”,
“union”,
“unsigned”,
“virtual”,
“void”,
“volatile”,
“while”
};
</PRE>
<!-- END CODE //-->
<P>In the 125 files that make up our DiffCAD program, the interface was never used to extend multiple interfaces. We find that assertions in other books—that the interface gives Java a kind of multiple inheritance—are groundless. Java has no multiple inheritance for implementations. Java has multiple inheritance of prototypes via interfaces. A good use for multiple inheritance of prototypes is as a workaround for Java’s strong typing, which constrains method invocation. We will see examples of this in Chapter 3.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="078-082.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="085-088.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -