📄 tij0090.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
#3.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
seems simple enough, but it doesn’t work. The problem is that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inherited from the common root class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which is what happens if you don’t specify a base class, thus all
classes are ultimately inherited from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
It is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hashCode( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that is used to generate the hash code for each object, and by default
it just uses the address of its object. Thus, the first instance of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog(3)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produce a hash code equal to the hash code for the second instance of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog(3)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that we tried to use as a lookup.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might think that all you need to do is write an appropriate override for <A NAME="Index804"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hashCode( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
But it still won’t work until you’ve done one more thing: override
the <A NAME="Index805"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that is also part of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This method is used by the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
when trying to determine if your key is equal to any of the keys in the table.
Again, the default
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object.equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
simply compares object addresses, so one
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog(3)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is not equal to another
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog(3)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thus,
to use your own classes as keys in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you must override both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hashCode( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
as shown in the following solution to the problem above:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: SpringDetector2.java</font>
<font color="#009900">// If you create a class that's used as a key in</font>
<font color="#009900">// a Hashtable, you must override hashCode()</font>
<font color="#009900">// and equals().</font>
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">class</font> Groundhog2 {
<font color="#0000ff">int</font> ghNumber;
Groundhog2(<font color="#0000ff">int</font> n) { ghNumber = n; }
<font color="#0000ff">public</font> <font color="#0000ff">int</font> hashCode() { <font color="#0000ff">return</font> ghNumber; }
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> equals(Object o) {
<font color="#0000ff">if</font> ((o != <font color="#0000ff">null</font>) && (o <font color="#0000ff">instanceof</font> Groundhog2))
<font color="#0000ff">return</font>
ghNumber == ((Groundhog2)o).ghNumber;
<font color="#0000ff">else</font> <font color="#0000ff">return</font> <font color="#0000ff">false</font>;
}
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> SpringDetector2 {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Hashtable ht = <font color="#0000ff">new</font> Hashtable();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 10; i++)
ht.put(<font color="#0000ff">new</font> Groundhog2(i),<font color="#0000ff">new</font> Prediction());
System.out.println("ht = " + ht + "\n");
System.out.println(
"Looking up prediction <font color="#0000ff">for</font> groundhog #3:");
Groundhog2 gh = <font color="#0000ff">new</font> Groundhog2(3);
<font color="#0000ff">if</font>(ht.containsKey(gh))
System.out.println((Prediction)ht.get(gh));
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that this uses the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class from the previous example, so
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SpringDetector.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must be compiled first or you’ll get a compile-time error when you try to
compile
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
SpringDetector2.java
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog2.hashCode( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns the ground hog number as an identifier. (In this example, the
programmer is responsible for ensuring that no two ground hogs exist with the
same ID number.) The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hashCode( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
not required to return a unique identifier, but the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method must be able to strictly determine whether two objects are equivalent.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method does two sanity checks: to see if the object is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and if not, whether it is an instance of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword, which is fully explained in Chapter 11). It should be a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">to
even continue executing
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The comparison, as you can see, is based on the actual
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ghNumber</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.
This time, when you run the program, you’ll see it produces the correct
output. (Many of the Java library classes override the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hashcode( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods to be based upon their contents.)
</FONT><P></DIV>
<A NAME="Heading258"></A><H4 ALIGN=LEFT>
Properties:
a type of Hashtable
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the first example in this book, a type of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
was used called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In that example, the lines:
</FONT><P></DIV>
<font color="#990000"><PRE>Properties p = System.getProperties();
p.list(System.out);</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">called
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method <A NAME="Index806"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getProperties( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to get a special
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object that described the system characteristics. The method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a method of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
sends the contents to any stream output that you choose. There’s also a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>save( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method to allow you to write your property list to a file in a way that it can
be retrieved later with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>load( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
the <A NAME="Index807"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is inherited from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it also
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>contains</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a second
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that acts to hold the list of “default” properties. So if a
property isn’t found in the primary list, the defaults will be searched.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is also available for use in your programs (an example is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ClassScanner.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in Chapter 17). You can find more complete details in the Java library
documentation.
</FONT><a name="_Toc375545358"></a><a name="_Toc408018574"></a><P></DIV>
<A NAME="Heading259"></A><H3 ALIGN=LEFT>
Enumerators
revisited
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">We
can now demonstrate the true power of the <A NAME="Index808"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
the ability to separate the operation of traversing a sequence from the
underlying structure of that sequence. In the following example, the class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintData</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
uses an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to move through a sequence and call the <A NAME="Index809"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method for every object. Two different types of collections are created, a <A NAME="Index810"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and a <A NAME="Index811"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and they are each filled with, respectively,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Mouse
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hamster
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">objects.
(These classes are defined earlier in the chapter; notice you must have compiled
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>HamsterMaze.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Work
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -