📄 071-075.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=071-075//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="068-071.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="075-078.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Null</B></FONT></P>
<P>One of the literals of Java is <I>null</I>. It is what you get when nothing has been created. For example:</P>
<!-- CODE SNIP //-->
<PRE>
if (some_object != null) {
System.out.println(“Object Exists!”);
}
</PRE>
<!-- END CODE SNIP //-->
<P><I>Null</I> has a null type and is the default value for any type that has not been created. For example:</P>
<!-- CODE SNIP //-->
<PRE>
class test {
int i[99];
}
test foo = new test();
</PRE>
<!-- END CODE SNIP //-->
<P>At this point, <I>foo.i</I> is equal to null. It is not until the memory is allocated for the array contained in the <I>foo</I> instance of the <I>test</I> class that the <I>i</I> field be non-null. For example the following code will set the <I>i</I> array, and <I>i</I> will no longer be null in value.</P>
<!-- CODE SNIP //-->
<PRE>
foo.i = new i[99];
</PRE>
<!-- END CODE SNIP //-->
<P><FONT SIZE="+1"><B>Casting</B></FONT></P>
<P>Type conversion in Java is called <I>casting</I>. When casting is performed, it is a run-time operation. Casting converts only between compatible types.</P>
<P>Sometimes the only way to know for certain when types are compatible is to run the program. If a <I>ClassCastException</I> is thrown at run time, the type conversion failed.</P>
<P>It is always correct to cast an instance from a subclass to its superclass. For example:</P>
<!-- CODE SNIP //-->
<PRE>
1. for (int i=0; i < v.size(); i++) {
2. s = (Shape) v.elementAt(i);
3. s.print();
4. }
</PRE>
<!-- END CODE SNIP //-->
<P>In line 1, an instance of a vector, <I>v</I> is accessed for size. The elements in the vector are accessed using line 2. Note that each element in the vector is a class that extends the <I>Shape</I> class. It is always correct to cast the subclass of the <I>Shape</I> class back into the superclass. This technique enables the <I>print()</I> method to be invoked on each shape in the vector instance.</P>
<P><FONT SIZE="+1"><B>Subclassing and Super</B></FONT></P>
<P>One feature of the Java class is that it can intrinsically represent taxonomic structures (such as those described in Chapter 1). The taxonomic structures are formed by Java classes when a subclass <I>extends</I> a superclass. This type of extension is called <I>direct inheritance</I>. Thus, in terms of knowledge representation, Java classes can represent the AKO (a-kind-of) relationship. In addition, Java classes can represent the has-a relationship using the class member variables. For example, we can represent the statement “A student is a-kind-of human” by creating a <I>student</I> class that extends the <I>human</I> class. We can also represent the statement “The student has-a pencil” by placing a class member variable of <I>pencil</I> class type into the <I>student</I> class construct. In the following section we present the syntax of Java and its relationship to the semantics of Java.</P>
<P>A class can be used to provide a container for an instance variable of any primitive type. For example:</P>
<!-- CODE SNIP //-->
<PRE>
class Lamp {
boolean on;
}
...
Lamp l = new Lamp ( );
l.on = true;
</PRE>
<!-- END CODE SNIP //-->
<P>A Java class can be used to store a reference to named constants:
</P>
<!-- CODE SNIP //-->
<PRE>
class Constants {
static final double PIon2 = Math.PI / 2;
}
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that these class examples have no methods. When one class extends another, we are subclassing a superclass. The subclass will inherit the member variables, and methods, of the superclass. In the case of a name conflict, the subclass implementation always overrides the superclass implementation. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
class Lamp extends Constants {
double power = 100 / PIon2; // watts
boolean on = true;
}
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>The <I>power</I> in the <I>Lamp</I> class is set using a <I>PIon2</I> constant that is inherited from the <I>Constants</I> class. In this case, it is not strictly correct to say that the <I>Lamp</I> is a-kind-of <I>Constants</I>. Thus, the <I>extends</I> is being used as a programming convenience and not a means of knowledge representation.<HR></FONT>
</BLOCKQUOTE>
<P>On the other hand, consider this example:
</P>
<!-- CODE SNIP //-->
<PRE>
class Student extends Human {
Pencil p;
}
class Human {
boolean bald = false;
}
</PRE>
<!-- END CODE SNIP //-->
<P>Now we represent the statement “Doug is a bald student with a pencil”:
</P>
<!-- CODE SNIP //-->
<PRE>
Student doug = new Student();
doug.p = new Pencil();
doug.bald = true;
</PRE>
<!-- END CODE SNIP //-->
<P><I>Super</I> is a keyword that permits a subclass to call upon the instance variable or method of the superclass. For example:</P>
<!-- CODE //-->
<PRE>
1. public class ClosableFrame extends Frame {
2. // constructor needed to pass window title to class Frame
3. public ClosableFrame(String name) {
4. // call java.awt.Frame(String) constructor
5. super(name);
6. }
7. // needed to allow window close
8. public boolean handleEvent(Event e) {
9. // Window Destroy event
10. if (e.id == Event.WINDOW_DESTROY) {
11. dispose();
12. return true;
13. }
14. // it’s good form to let the superclass look
at any unhandled events
15. return super.handleEvent(e);
16. } // end handleEvent()
17. } // end class ClosableFrame
</PRE>
<!-- END CODE //-->
<P>Line 15 of class <I>ClosableFrame</I> invokes <I>super.handleEvent</I> because the event that was passed, <I>e</I>, may not have been an <I>Event.WINDOW_DESTROY</I> event. In that case, it may be that the superclass can decode and handle the event properly. A frame that extends the <I>ClosableFrame</I> will inherit the ability to handle the <I>Event.WINDOW_DESTROY</I> events by invoking <I>super.handleEvent(e)</I>.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="068-071.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="075-078.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 + -