📄 068-071.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=068-071//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="064-068.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="071-075.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Overloaded Methods</B></FONT></P>
<P>As mentioned before, Java has no functions, only methods. Methods provide the algorithm for manipulating data that is stored in the class member variables. The constructor is a method. If no constructor is specified, a default constructor is provided. The default constructor takes <I>null</I> as an argument. The default constructor is overridden when another constructor is specified. For example:</P>
<!-- CODE //-->
<PRE>
class Lamp {
boolean on;
int Wattage;
Lamp (int w) {
Wattage = w;
}
Lamp ( ) {
Wattage = 100;
}
}
</PRE>
<!-- END CODE //-->
<P>The <I>Lamp</I> constructor has been over loaded with two versions. The first version will support the following:</P>
<!-- CODE SNIP //-->
<PRE>
Lamp dim = new Lamp(40);
</PRE>
<!-- END CODE SNIP //-->
<P>The second version supports the constructor invocation:
</P>
<!-- CODE SNIP //-->
<PRE>
Lamp bright = new Lamp();
</PRE>
<!-- END CODE SNIP //-->
<P>Java requires that each method have a unique signature. The signature of the method is determined by the number of arguments and their compile-time types.
</P>
<P><FONT SIZE="+1"><B>Static Methods</B></FONT></P>
<P>Classes that contain static fields have only one copy (or instance) of the static member variable. The static member variable is therefore global to all instances of the class. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
import java.awt.*;
import java.applet.Applet;
class apple {
static int numberOfApples = 0;
apple() { numberOfApples++ ;}
}
</PRE>
<!-- END CODE SNIP //-->
<P>Every time a new <I>apple</I> instance is made, the <I>numberOfApples</I> member will be incremented. Furthermore, all instances of the <I>apple</I> class will be able to access the same <I>numberOfApples</I> field. Thus, the <I>numberOfApples</I> int is stored in only a single place in memory.</P>
<P>Static members are instantiated when the class is initialized. They can not throw an exception without creating a compile-time error. Also, they can not refer to variables that have not been defined. The following code results in a compile-time error:</P>
<!-- CODE SNIP //-->
<PRE>
public class Lamp {
static int wattage = voltage*current;
static int voltage = 110;
static int current = 1;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The error looks like this:
</P>
<!-- CODE SNIP //-->
<PRE>
Error : Can’t make forward reference to voltage in class Lamp.
Lamp.java line 2 static int wattage = voltage*current;
</PRE>
<!-- END CODE SNIP //-->
<P>On the other hand, if <I>power</I> is computed by a method, no such check is performed, although perhaps it should be. For example:</P>
<!-- CODE //-->
<PRE>
public class Lamp {
static int wattage = power();
static int voltage = 110;
static int current = 1;
static int power() {
return voltage * current;
}
public static void main(String argv[]) {
Lamp i = new Lamp();
System.out.println(“The power is “+
i.wattage);
}
}
</PRE>
<!-- END CODE //-->
<P>The preceding code prints the following:
</P>
<!-- CODE SNIP //-->
<PRE>
The power is 0
</PRE>
<!-- END CODE SNIP //-->
<P>Why should <I>power</I> be zero? <I>Power</I> is <I>current</I> times <I>voltage</I> and both are set to nonzero values. But, it compiles and runs. So what gives? The answer is the order of dependency. The <I>power</I> method is invoked first. The values for <I>voltage</I> and <I>current</I> are unset (and default to zero). Thus, <I>power</I> is set to the proper 110 watt answer only if both <I>current</I> and <I>voltage</I> are defined first. For example:</P>
<!-- CODE //-->
<PRE>
public class Lamp {
static int voltage = 110;
static int current = 1;
static int wattage = power();
static int power() {
return voltage * current;
}
public static void main(String argv[]) {
Lamp i = new Lamp();
System.out.println(“The power is “+
i.wattage);
}
}
</PRE>
<!-- END CODE //-->
<P>The preceding code prints the following:
</P>
<!-- CODE SNIP //-->
<PRE>
The power is 110
</PRE>
<!-- END CODE SNIP //-->
<P>There are two points to learn from this example:
</P>
<DL>
<DD><B>1.</B> Static variables must be defined in the order of independent first, dependent second.
<DD><B>2.</B> Static methods cannot be used to avoid constraint 1.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="064-068.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="071-075.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 + -