078-082.html
来自「dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh」· HTML 代码 · 共 242 行
HTML
242 行
<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=078-082//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="075-078.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="082-085.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Packaging is essential for writing large programs. Once upon a time, in a fit of quick prototyping frenzy, a programmer (who shall remain nameless) decided to write many classes without placing them in a package. The sophisticated reader will never do this. Everything worked fine, the days were sunny, and the programmer was productive. Then one day the program got so big and intertwined that even the programmer had trouble sorting out the interdependencies. “If only packaging had been enforced. Then I could have packaged as I went along,” he thought. The sun began to set as his well-intentioned efforts to introduce packages into a large system began to fail. Even today, the programmer is trying to grapple with this massive code block. His productivity has fallen. The sun no longer shines. He has taken to drinking lots of java. His blood pressure has shot up as he became a Javaholic. This led to no good and he soon writing books on Java.
</P>
<P>The moral of the story: Package small, package often!</P>
<P><FONT SIZE="+1"><B>Import</B></FONT></P>
<P><I>Import</I> is a reserved keyword in Java that permits packages to be brought into the name space. Like libraries in C, packages are used to organize Java program libraries and to control type access. For example, a class or interface type is visible only outside a package if it is declared as <I>public</I>. The required Java packages are <I>java.lang</I>, <I>java.util</I> and <I>java.io</I>. The MBNF for the <I>import</I> statement follows:</P>
<!-- CODE SNIP //-->
<PRE>
importStatement ->
“import” ( ( packageName “.” “*”
“;” ) |
( className | interfaceName ) ) “;” .
</PRE>
<!-- END CODE SNIP //-->
<P>Extra <I>imports</I> are discarded but can make compilation take longer. For example:</P>
<!-- CODE SNIP //-->
<PRE>
import java.io.*;
import java.util.*;
</PRE>
<!-- END CODE SNIP //-->
<P>Import all the public classes and interfaces in the <I>java.io</I> and <I>java.util</I> packages. It is possible to import a single class or interface from a package and thereby prevent a name-space conflict. For example:</P>
<!-- CODE SNIP //-->
<PRE>
import java.io.FilenameFilter;
import java.io.File;
</PRE>
<!-- END CODE SNIP //-->
<P>These two classes are generally the only ones needed to build a <I>FilenameFilter</I>. You can build a package by the use of the <I>package</I> statement.</P>
<P><FONT SIZE="+1"><B>Visibility</B></FONT></P>
<P>Visibility is the aspect of an identifier that permits access to the name space that contains the identifier. If an identifier is visible, it may collide with another identifier. This is called name-space contention. Furthermore, if a method is not visible it cannot be invoked. The MBNF for the modifier is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
modifier ->
“public” | “private” | “protected” | “static” | “final” |
“native”
“synchronized” | “abstract” | “threadsafe” | “transient” .
</PRE>
<!-- END CODE SNIP //-->
<P>The visibility modifier is a subset consisting of the following
</P>
<!-- CODE SNIP //-->
<PRE>
visibility_modifier ->
“public” | “private” | “protected” .
</PRE>
<!-- END CODE SNIP //-->
<DL>
<DD><B>•</B> <I>public</I>: gives the world access to your class, method, or field variable. It is required for those types you wish to advertise for use outside your package. The <I>public</I> modifier applies to classes, methods, and variables.
<DD><B>•</B> <I>protected</I>: gives access to the class, subclass, and package. It can be applied only to methods and variables.
<DD><B>•</B> <I>default</I>: gives package access. Classes that import the package will not be able to access types that have default access. To give such access, the type must be declared as public.
<DD><B>•</B> <I>private protected</I>: gives class and subclass access. It can be applied only to methods and variables. <I>Private protected</I> prevents package access and is slated to be removed with JDK 1.1.
<DD><B>•</B> <I>private</I>: gives access only within this class. Private access prevents inheritance. It can be applied only to methods and variables.
</DL>
<P>It is idiomatic in Java to have a collection of public static methods contained in a public final class with a private constructor that is never invoked. The <I>Math</I> package is like this, as is the DiffCAD file utility package called <I>futil</I>. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public final class futil {
/**
* Don’t let anyone instantiate this class.
*/
private futil() {}
</PRE>
<!-- END CODE SNIP //-->
<P>Table 2.2 shows the visibility matrix for Java.
</P>
<TABLE BORDER WIDTH="100%"><CAPTION><B>Table 2.2</B> Visibility Matrix
<TR>
<TH WIDTH="13%">
<TH WIDTH="13%" ALIGN="LEFT">Access by non-subclass from same package
<TH WIDTH="13%" ALIGN="LEFT">Access to non-subclass from different package
<TH WIDTH="13%" ALIGN="LEFT">Access to subclass from different package
<TH WIDTH="13%" ALIGN="LEFT" VALIGN="TOP">Inherited by subclass in same package
<TH WIDTH="25%" ALIGN="LEFT" COLSPAN="2" VALIGN="TOP">Inherited by subclass in different package
<TH WIDTH="10%" ALIGN="LEFT" VALIGN="TOP">w=
<TR>
<TD>private
<TD>0
<TD>0
<TD>0
<TD>0
<TD>0
<TD>0
<TD>0
<TR>
<TD>private protected
<TD VALIGN="TOP">0
<TD VALIGN="TOP">0
<TD VALIGN="TOP">0
<TD VALIGN="TOP">0
<TD VALIGN="TOP">1
<TD VALIGN="TOP">1
<TD VALIGN="TOP">2
<TR>
<TD>default
<TD>1
<TD>1
<TD>0
<TD>0
<TD>1
<TD>0
<TD>3
<TR>
<TD>protected
<TD>1
<TD>1
<TD>0
<TD>0
<TD>1
<TD>1
<TD>4
<TR>
<TD>public
<TD>1
<TD>1
<TD>1
<TD>1
<TD>1
<TD>1
<TD>6
</TABLE>
<P>A “1” in the visibility matrix indicates a true condition, a "0" indicates a false condition. The row is summed to obtain a “weight” for the visibility vector. The weight is shown in the <I>w</I> column. For example, <I>private</I> gives no access outside the class, so the weight is zero. <I>Public</I> gives you complete access, so the weight is 6. Here is an example of the use and abuse of the visibility modifiers:</P>
<!-- CODE //-->
<PRE>
public class visible {
public String publicString = “publicString”;
String defaultString =”defaultString”;
protected String protectedString =
“protectedString”;
private protected String privateProtectedString =
“privateProtectedString”;
private String privateString = “privateString”;
// What follows is not in the Spec...it just worked here.
private public String privatePublicString =
“privatePublic”;
//gosh!
public private String publicPrivateString =
“publicPrivate”;
//a stranger in a stranger land...
protected public String protectedPublicString =
“protectedPublic”;
//can you stand it?
public protected String publicProtectedString =
“publicProtected”;
//art is what you get away with.
public protected private String publicProtectedPrivateString
= “publicProtectedPrivate”;
}
public class invisible extends visible {
String iGetPublicStrings = publicString;
String iGetDefaultStrings = defaultString;
String iGetProtectedStrings = protectedString;
String iGetPrivateProtectedStrings = privateProtectedString;
// String iDontGetPrivateStrings = privateString;
String iGetPrivatePublicStrings = privatePublicString;
String iGetpublicPrivateStrings = publicPrivateString;
String iGetProtectedPublicStrings = protectedPublicString;
String iGetPublicProtectedStrings = publicProtectedString;
String iGetPublicProtectedPrivateStrings =
publicProtectedPrivateString;
}
</PRE>
<!-- END CODE //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>The <I>public</I> modifier appears to override <I>private</I> no matter what the order.<HR></FONT>
</BLOCKQUOTE>
<P>This is not a part of the specification for Java. It just appears to work. It is probably within the vendor’s right to declare a private public declaration a semantic error.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="075-078.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="082-085.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 + =
减小字号Ctrl + -
显示快捷键?