📄 064-068.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=064-068//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="060-064.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="068-071.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Named Constants</FONT></H4>
<P><I>Constants</I> are values that remain unchanged during their life. For example:</P>
<!-- CODE SNIP //-->
<PRE>
static final double PI = 3.14159265358979323846;
</PRE>
<!-- END CODE SNIP //-->
<P>The term <I>static</I> is known as a modifier. It indicates that there is to be only one incarnation of the field. <I>Final</I> indicates that <I>PI</I> cannot change during the life of the <I>PI</I> variable. Assignment of a <I>final</I> field is a compile-time error. You may, if you like, restrict the visibility of the <I>static final</I> field by using a modifier prefix. You can perform computations with knowns on the right-hand side of the equals sign. For example:</P>
<!-- CODE SNIP //-->
<PRE>
private final double pi_2 = Math.PI * 2;
</PRE>
<!-- END CODE SNIP //-->
<P>Here we see that the field <I>pi_2</I> has a <I>private</I> visibility. This means that the <I>pi_2</I> name is not visible outside the class (a discussion we defer until later). Any valid type specifier may follow the final modifier. The MBNF for this is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
typeSpecifier ->
“boolean” | “byte” | “char” |
“short” | “int” | “float” |
“long” | “double” | className |
interfaceName .
</PRE>
<!-- END CODE SNIP //-->
<P>The following are examples of the final modifier:
</P>
<!-- CODE SNIP //-->
<PRE>
static final int HORIZONTAL = 0;
static final String version = “1.0”;
public static final int ACC_PUBLIC = 0x1;
static final float twoPI = (float) (2*Math.PI);
public static final char NOT_CODE = (char)12;
private static final long UNIT = 1000;
</PRE>
<!-- END CODE SNIP //-->
<P>Note the use of 0x1 to denote hexadecimal code. Also note the use of the cast operators (<I>float</I>) and (<I>char</I>). Casting is run-time type conversion and is discussed in more detail later in the chapter.</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Classes</FONT></H4>
<P>This section introduces a reference type known as a <I>class</I>. A class is a combination of code (methods) and data (variables) joined into a single entity. A class is essentially a description of how to make an instance of an object. This is what makes Java object-oriented. Every instance has a class that is used to determine how to create the instance, which variables the instance will contain, and which messages the instance will respond to. Instances of classes are of reference type. In Java a reference is just like a pointer, except that no pointer arithmetic is permitted.</P>
<P>The following is a summary of the MBNF needed to declare a class:</P>
<!-- CODE //-->
<PRE>
classDeclaration ->
< modifier > “class” identifier [ “extends” className] [ “implements”
interfaceName < “,” interfaceName > ] “{“ < fieldDeclaration > “}”.
modifier ->
“public” | “private” | “protected” |
“static” | “final” |
“native” | “synchronized” | “abstract” | “threadsafe” |
“transient” .
identifier ->
“a..z,$,_” < “a..z,$,_,0..9,unicode character over 00C0” > .
className ->
identifier | ( packageName “.” identifier ) .
interfaceName ->
identifier | ( packageName “.” identifier ) .
fieldDeclaration ->
( [docComment] ( methodDeclaration | constructorDeclaration |
variableDeclaration ) ) | staticInitializer | “;” .
</PRE>
<!-- END CODE //-->
<P>In Java, a class can define both the data structure and the algorithm for manipulating the data structure. The class name consists of an identifier (which can be of any length), and the class which may <I>extend</I> another class. In Java, classes can for an AKO (a-kind-of) taxonomy, as described in Chapter 1.</P>
<!-- CODE SNIP //-->
<PRE>
public class AppletFrame extends Frame {}
</PRE>
<!-- END CODE SNIP //-->
<P>An instance of a class has a type. The <I>new</I> operator is used to make an instance of a class. For example:</P>
<!-- CODE SNIP //-->
<PRE>
class point {
public double x,y;
}
point p1 = new point();
p1.x = 10;
p1.y = 11;
</PRE>
<!-- END CODE SNIP //-->
<P>To gain access to the Java class libraries, you must import using an <I>import</I> statement. For example:</P>
<!-- CODE SNIP //-->
<PRE>
import java.awt.*;
import java.applet.Applet;
</PRE>
<!-- END CODE SNIP //-->
<P><I>Import</I> is described in more detail later in this chapter. To build on the methods and data structures of a parent class, a subclass is constructed that <I>extends</I> the parent class. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public class AppletFrame extends Frame {
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, the <I>AppletFrame</I> class extends the <I>Frame</I> class. This means that the <I>AppletFrame</I> is a kind of <I>Frame</I>. Sometimes we would like to have a class that can never be instanced, only extended. We would declare the class as <I>abstract</I>. For example:</P>
<!-- CODE SNIP //-->
<PRE>
abstract class AppletUtil {
</PRE>
<!-- END CODE SNIP //-->
<P><I>AppletUtil</I> is an abstract class. We might like to keep <I>AppletUtil</I> abstract because it contains a combination of methods and fields that are to be inherited or overridden by a subclass. For example:</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
abstract class AppletUtil {
static Frame appletFrame = new Frame();
static void run ( Applet applet) {
appletFrame.addNotify();
appletFrame.add(“Center”,applet);
appletFrame.resize(400,400);
applet.init();
appletFrame.show();
applet.start();
}
}
</PRE>
<!-- END CODE //-->
<P>In this case, we can never make an instance of the <I>AppletUtil</I> class, because it is abstract. However, we can access the <I>appletFrame</I> variable and even invoke the <I>run</I> method without ever making an instance. For example, the following will run an instance of the <I>Applet</I> class, called <I>a</I>.</P>
<!-- CODE SNIP //-->
<PRE>
AppletUtil.run(a);
</PRE>
<!-- END CODE SNIP //-->
<P>This example introduces the basic concept of a <I>method</I>. Classes in Java have two possible members: fields and methods. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public class IHateHelloWorldExamples {
public static void main(String args[]) {
System.out.println(“hello world”);
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>Here we see a class called <I>IHateHelloWorldExamples</I>. A Java application, <I>IHateHelloWorldExamples</I> contains a <I>main</I> method that is invoked at run time. This causes the “<I>hello world</I>” string to be printed on the console. The following, more elaborate example shows several numbered lines. The line numbers are for reference only. Lines 1 and 2 are used to import the Java class libraries.</P>
<!-- CODE SNIP //-->
<PRE>
1. import java.util.*;
2. import java.awt.*;
</PRE>
<!-- END CODE SNIP //-->
<P>Line 3 shows one class subclassing (also known as extending) another class. Line 4 shows a class method with no return, not even <I>void</I>. This class method has the same name as the class and is called the <I>constructor</I>. The constructor returns an instance of the class when <I>new</I> is invoked.</P>
<!-- CODE SNIP //-->
<PRE>
3. class Camera_grating_line extends Shape {
4. Camera_grating_line() {
5. color =
6. Color.blue;
7. }...
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Camera_grating_line</I> class is a kind of <I>Shape</I>. It contains one method and no fields. The <I>color</I> field is stored in the <I>Shape</I> base class.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="060-064.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="068-071.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 + -