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

📄 075-078.html

📁 dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh ghghf hgf hghg gh fg hg hfg hfh f hg hgfh gkjh kjkh g yj f
💻 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=075-078//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="071-075.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="078-082.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Abstract Classes and Methods</B></FONT></P>
<P>An abstract class is one that can never be instanced. It is a compile-time error to attempt to instantiate an abstract class. An abstract class must be extended before it can be instantiated. You can make a class abstract by declaring the class as abstract or by declaring a method within the class as abstract. Another way to declare an abstract class is to extend an interface or abstract class without providing an implementation for the abstract methods in the interface or superclass.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>A class should be declared abstract only if you intend to create subclasses to complete the implementation.<HR></FONT>
</BLOCKQUOTE>
<P>Abstract classes are used to create superclasses that can be extended. This situation typically involves the implementation of some, but not all of a class&#146;s methods. Methods without an implementation are declared abstract. Abstract methods must be implemented by a subclass before the subclass can be instanced. If a subclass does not implement the abstract methods, the subclass is also an abstract class even if it is not explicitly declared as abstract. Java supports polymorphism by casting instances into their common superclasses. The superclasses typically have abstract methods that are implemented by the subclasses. For example, in DiffCAD there is an abstract class called <I>Shape</I>:</P>
<!-- CODE SNIP //-->
<PRE>
 abstract class Shape extends computation &#123;
...
   abstract void draw(Graphics g);
...
</PRE>
<!-- END CODE SNIP //-->
<P>The numerous kinds of <I>Shapes</I> in DiffCAD are stored in a vector instance called <I>drawnShapes.</I> Polymorphism is performed when the <I>Shape</I> subclass instances are accessed in the <I>drawnShapes</I> vector, cast into their <I>Shape</I> superclass, and then used as the target of the <I>draw</I> method. For example:</P>
<!-- CODE SNIP //-->
<PRE>
    Shape s;
    for (int i = 0; i &lt; drawnShapes.size(); i&#43;&#43;) &#123;
         s = (Shape)drawnShapes.elementAt(i);
         s.draw(g);
      &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Shape polymorphism is possible for any of the methods (even the non-abstract ones) in the <I>Shape</I> class.</P>
<P><FONT SIZE="+1"><B>Final Classes and Methods</B></FONT></P>
<P>In contrast with abstract classes, in which a class must be subclassed to be extended, there are <I>final</I> classes. A class that is declared as final cannot be extended, although it can be instanced. Fields in a class that are declared as final become constant throughout the life of the class.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>To prevent a final class from being instanced, declare a single private constructor and never instance the class internally.<HR></FONT>
</BLOCKQUOTE>
<P>For example:
</P>
<!-- CODE SNIP //-->
<PRE>
public final class Audio &#123;

     // Prevent instantiation
     private Audio() &#123;&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>final</I> modifier prevents the <I>Audio</I> class from being extended. The <I>private</I> modifier makes the constructor invisible. Furthermore, the existence of a constructor will override the default constructor. Thus, no method outside the <I>Audio</I> class can instantiate the <I>Audio</I> class.</P>
<P><I>Final</I> can also be used as a method modifier, preventing the method from being overridden. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public final boolean keyDown(Event e, int key) &#123;
...
</PRE>
<!-- END CODE SNIP //-->
<P>This approach may lead to a compiler optimization as well as prevent any subclass from overriding the <I>keyDown</I> implementation.</P>
<P>When <I>final</I> is used on a class, type checking can occur at compile time.</P>
<P><FONT SIZE="+1"><B>Packages</B></FONT></P>
<P>The <I>package</I> statement is used to isolate the class and interface name space so that only public classes and interfaces will be used by nonpackage programmers. This technique restricts default access of classes and interfaces to package programmers.</P>
<P>The MBNF for packages follows:</P>
<!-- CODE SNIP //-->
<PRE>
packageStatement -&gt;
&#147;package&#148; packageName  &#147;;&#148;  .
packageName -&gt;
identifier   |  ( packageName  &#147;.&#148; identifier )  .
</PRE>
<!-- END CODE SNIP //-->
<P>When you&#146;re building a large program, it is often to your advantage to divide it into subsections, each of which resides in a package. For example, DiffCAD has a Hypertext Markup Language (HTML) generator. HTML is a common file format that is read by browsers on the World Wide Web. The DiffCAD HTML generator reads C, C&#43;&#43; or Java code and outputs a colorized version of the source in HTML. The generator, which consists of 15 source files, has a single public class with a single public constructor. Anything that is not declared public in the package (i.e., anything that has private or default visibility) will not be visible even after the import statement is issued. The error
</P>
<!-- CODE SNIP //-->
<PRE>
Error   : No constructor matching HtmlGenerator(java.lang.String)
 found in class htmlconverter.HtmlGenerator.
process_menuitem.java line 21      new HtmlGenerator(&#147;The
                                    HTML Generator&#148;);
</PRE>
<!-- END CODE SNIP //-->
<P>is emitted when
</P>
<!-- CODE SNIP //-->
<PRE>
HtmlGenerator hg =
     new HtmlGenerator(&#147;The HTML Generator&#148;);
     hg.main(null);
</PRE>
<!-- END CODE SNIP //-->
<P>is processed by the compiler, because the default visibility is not sufficient for access outside of the package. Only by providing the <I>public</I> accessor for the constructor can the HtmlGenerator class be instanced. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public HtmlGenerator(String title) &#123;
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>A package permits a grouping of multiple classes and interfaces into a unit that can be developed and deployed separately.<HR></FONT>
</BLOCKQUOTE>
<P>The declaration of <I>public</I> from within a class is an explicit advertisement to those who would import your package. In effect, it says &#147;You need this to use my package.&#148;</P>
<P>When classes are written into a source file with no package declaration, they become a part of the unnamed package. Code that resides in packages should not use code in an unnamed package. Separate compilation units are a design goal of Java developers, and packages are the mechanism for their creation.</P>
<P>Every class must reside in a compilation unit. An unnamed package is, in effect, an unnamed compilation unit. It is probably a Java design flaw to permit unnamed compilation units to compile.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="071-075.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="078-082.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>

<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright &copy; <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 + -