📄 tij0120.html
字号:
example can be rewritten using Java 1.1<A NAME="Index1388"></A>
class literals<A NAME="Index1389"></A><A NAME="Index1390"></A>.
The result is cleaner in many ways:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: PetCount2.java</font>
<font color="#009900">// Using Java 1.1 class literals</font>
<font color="#0000ff">package</font> c11.petcount2;
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">class</font> Pet {}
<font color="#0000ff">class</font> Dog <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Pug <font color="#0000ff">extends</font> Dog {}
<font color="#0000ff">class</font> Cat <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Rodent <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Gerbil <font color="#0000ff">extends</font> Rodent {}
<font color="#0000ff">class</font> Hamster <font color="#0000ff">extends</font> Rodent {}
<font color="#0000ff">class</font> Counter { <font color="#0000ff">int</font> i; }
<font color="#0000ff">public</font> <font color="#0000ff">class</font> PetCount2 {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Vector pets = <font color="#0000ff">new</font> Vector();
Class[] petTypes = {
<font color="#009900">// Class literals work in Java 1.1+ only:</font>
Pet.<font color="#0000ff">class</font>,
Dog.<font color="#0000ff">class</font>,
Pug.<font color="#0000ff">class</font>,
Cat.<font color="#0000ff">class</font>,
Rodent.<font color="#0000ff">class</font>,
Gerbil.<font color="#0000ff">class</font>,
Hamster.<font color="#0000ff">class</font>,
};
<font color="#0000ff">try</font> {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 15; i++) {
<font color="#009900">// Offset by one to eliminate Pet.class:</font>
<font color="#0000ff">int</font> rnd = 1 + (<font color="#0000ff">int</font>)(
Math.random() * (petTypes.length - 1));
pets.addElement(
petTypes[rnd].newInstance());
}
} <font color="#0000ff">catch</font>(InstantiationException e) {}
<font color="#0000ff">catch</font>(IllegalAccessException e) {}
Hashtable h = <font color="#0000ff">new</font> Hashtable();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < petTypes.length; i++)
h.put(petTypes[i].toString(),
<font color="#0000ff">new</font> Counter());
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < pets.size(); i++) {
Object o = pets.elementAt(i);
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Pet)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Pet")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Dog)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Dog")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Pug)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Pug")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Cat)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Cat")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Rodent)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Rodent")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Gerbil)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Gerbil")).i++;
<font color="#0000ff">if</font>(o <font color="#0000ff">instanceof</font> Hamster)
((Counter)h.get(
"<font color="#0000ff">class</font> c11.petcount2.Hamster")).i++;
}
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < pets.size(); i++)
System.out.println(
pets.elementAt(i).getClass().toString());
Enumeration keys = h.keys();
<font color="#0000ff">while</font>(keys.hasMoreElements()) {
String nm = (String)keys.nextElement();
Counter cnt = (Counter)h.get(nm);
System.out.println(
nm.substring(nm.lastIndexOf('.') + 1) +
" quantity: " + cnt.i);
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here,
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>typenames</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
array has been removed in favor of getting the type name strings from the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. Notice the extra work for this: the class name is not, for example,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Gerbil,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
but instead
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c11.petcount2.Gerbil</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
since the package name is included. Notice also that the system can distinguish
between classes and interfaces.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can also see that the creation of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>petTypes</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does not need to be surrounded by a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>try</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
block since it’s evaluated at compile time and thus won’t throw any
exceptions, unlike
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class.forName( )</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">When
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Pet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are dynamically created, you can see that the random number is
restricted so it is between 1 and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>petTypes.length</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and does not include zero. That’s because zero refers to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Pet.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and presumably a generic
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Pet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is not interesting. However, since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Pet.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is part of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>petTypes</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the result is that all of the pets get counted.
</FONT><P></DIV>
<A NAME="Heading358"></A><H4 ALIGN=LEFT>
A
dynamic instanceof
<P><A NAME="Index1391"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index1392"></A>
has added the <A NAME="Index1393"></A><A NAME="Index1394"></A><A NAME="Index1395"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isInstance</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method to the class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This allows you to dynamically call the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
operator, which you could do only statically in Java 1.0<A NAME="Index1396"></A>
(as previously shown). Thus, all those tedious
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statements can be removed in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PetCount</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
example:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: PetCount3.java</font>
<font color="#009900">// Using Java 1.1 isInstance()</font>
<font color="#0000ff">package</font> c11.petcount3;
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">class</font> Pet {}
<font color="#0000ff">class</font> Dog <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Pug <font color="#0000ff">extends</font> Dog {}
<font color="#0000ff">class</font> Cat <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Rodent <font color="#0000ff">extends</font> Pet {}
<font color="#0000ff">class</font> Gerbil <font color="#0000ff">extends</font> Rodent {}
<font color="#0000ff">class</font> Hamster <font color="#0000ff">extends</font> Rodent {}
<font color="#0000ff">class</font> Counter { <font color="#0000ff">int</font> i; }
<font color="#0000ff">public</font> <font color="#0000ff">class</font> PetCount3 {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Vector pets = <font color="#0000ff">new</font> Vector();
Class[] petTypes = {
Pet.<font color="#0000ff">class</font>,
Dog.<font color="#0000ff">class</font>,
Pug.<font color="#0000ff">class</font>,
Cat.<font color="#0000ff">class</font>,
Rodent.<font color="#0000ff">class</font>,
Gerbil.<font color="#0000ff">class</font>,
Hamster.<font color="#0000ff">class</font>,
};
<font color="#0000ff">try</font> {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 15; i++) {
<font color="#009900">// Offset by one to eliminate Pet.class:</font>
<font color="#0000ff">int</font> rnd = 1 + (<font color="#0000ff">int</font>)(
Math.random() * (petTypes.length - 1));
pets.addElement(
petTypes[rnd].newInstance());
}
} <font color="#0000ff">catch</font>(InstantiationException e) {}
<font color="#0000ff">catch</font>(IllegalAccessException e) {}
Hashtable h = <font color="#0000ff">new</font> Hashtable();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < petTypes.length; i++)
h.put(petTypes[i].toString(),
<font color="#0000ff">new</font> Counter());
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < pets.size(); i++) {
Object o = pets.elementAt(i);
<font color="#009900">// Using isInstance to eliminate individual</font>
<font color="#009900">// instanceof expressions:</font>
<font color="#0000ff">for</font> (<font color="#0000ff">int</font> j = 0; j < petTypes.length; ++j)
<font color="#0000ff">if</font> (petTypes[j].isInstance(o)) {
String key = petTypes[j].toString();
((Counter)h.get(key)).i++;
}
}
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < pets.size(); i++)
System.out.println(
pets.elementAt(i).getClass().toString());
Enumeration keys = h.keys();
<font color="#0000ff">while</font>(keys.hasMoreElements()) {
String nm = (String)keys.nextElement();
Counter cnt = (Counter)h.get(nm);
System.out.println(
nm.substring(nm.lastIndexOf('.') + 1) +
" quantity: " + cnt.i);
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the Java 1.1<A NAME="Index1397"></A>
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isInstance( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method has eliminated the need for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
expressions. In addition, this means that you can add new types of pets simply
by changing the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>petTypes</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
array; the rest of the program does not need modification (as it did when 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">
expressions).
</FONT><a name="_Toc375545407"></a><a name="_Toc408018648"></a><a name="_Toc305593313"></a><a name="_Toc305628785"></a><a name="_Toc312374146"></a><a name="_Toc375545409"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0119.html">Prev</a> | <a href="tij0121.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -