📄 tij0186.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0185.html">Prev</a> | <a href="tij0187.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
A
method lookup tool
<P><A NAME="Index3062"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Chapter
11 introduced the Java 1.1 concept of <A NAME="Index3063"></A><A NAME="Index3064"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>reflection</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and used that feature to look up methods for a particular class – either
the entire list of methods or a subset of those whose names match a keyword you
provide. The magic of this is that it can automatically show you
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>all</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the methods for a class without forcing you to walk up the inheritance
hierarchy examining the base classes at each level. Thus, it provides a
valuable timesaving tool for programming: because the names of most Java method
names are made nicely verbose and descriptive, you can search for the method
names that contain a particular word of interest. When you find what you think
you’re looking for, check the online documentation.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">However,
by Chapter 11 you hadn’t seen the AWT, so that tool was developed as a
command-line application. Here is the more useful GUI version, which
dynamically updates the output as you type and also allows you to cut and paste
from the output:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: DisplayMethods.java</font>
<font color="#009900">// Display the methods of any class inside</font>
<font color="#009900">// a window. Dynamically narrows your search.</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">import</font> java.lang.reflect.*;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> DisplayMethods <font color="#0000ff">extends</font> Applet {
Class cl;
Method[] m;
Constructor[] ctor;
String[] n = <font color="#0000ff">new</font> String[0];
TextField
name = <font color="#0000ff">new</font> TextField(40),
searchFor = <font color="#0000ff">new</font> TextField(30);
Checkbox strip =
<font color="#0000ff">new</font> Checkbox("Strip Qualifiers");
TextArea results = <font color="#0000ff">new</font> TextArea(40, 65);
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
strip.setState(<font color="#0000ff">true</font>);
name.addTextListener(<font color="#0000ff">new</font> NameL());
searchFor.addTextListener(<font color="#0000ff">new</font> SearchForL());
strip.addItemListener(<font color="#0000ff">new</font> StripL());
Panel
top = <font color="#0000ff">new</font> Panel(),
lower = <font color="#0000ff">new</font> Panel(),
p = <font color="#0000ff">new</font> Panel();
top.add(<font color="#0000ff">new</font> Label("Qualified <font color="#0000ff">class</font> name:"));
top.add(name);
lower.add(
<font color="#0000ff">new</font> Label("String to search <font color="#0000ff">for</font>:"));
lower.add(searchFor);
lower.add(strip);
p.setLayout(<font color="#0000ff">new</font> BorderLayout());
p.add(top, BorderLayout.NORTH);
p.add(lower, BorderLayout.SOUTH);
setLayout(<font color="#0000ff">new</font> BorderLayout());
add(p, BorderLayout.NORTH);
add(results, BorderLayout.CENTER);
}
<font color="#0000ff">class</font> NameL <font color="#0000ff">implements</font> TextListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> textValueChanged(TextEvent e) {
String nm = name.getText().trim();
<font color="#0000ff">if</font>(nm.length() == 0) {
results.setText("No match");
n = <font color="#0000ff">new</font> String[0];
<font color="#0000ff">return</font>;
}
<font color="#0000ff">try</font> {
cl = Class.forName(nm);
} <font color="#0000ff">catch</font> (ClassNotFoundException ex) {
results.setText("No match");
<font color="#0000ff">return</font>;
}
m = cl.getMethods();
ctor = cl.getConstructors();
<font color="#009900">// Convert to an array of Strings:</font>
n = <font color="#0000ff">new</font> String[m.length + ctor.length];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < m.length; i++)
n[i] = m[i].toString();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < ctor.length; i++)
n[i + m.length] = ctor[i].toString();
reDisplay();
}
}
<font color="#0000ff">void</font> reDisplay() {
<font color="#009900">// Create the result set:</font>
String[] rs = <font color="#0000ff">new</font> String[n.length];
String find = searchFor.getText();
<font color="#0000ff">int</font> j = 0;
<font color="#009900">// Select from the list if find exists:</font>
<font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0; i < n.length; i++) {
<font color="#0000ff">if</font>(find == <font color="#0000ff">null</font>)
rs[j++] = n[i];
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(n[i].indexOf(find) != -1)
rs[j++] = n[i];
}
results.setText("");
<font color="#0000ff">if</font>(strip.getState() == <font color="#0000ff">true</font>)
<font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0; i < j; i++)
results.append(
StripQualifiers.strip(rs[i]) + "\n");
<font color="#0000ff">else</font> <font color="#009900">// Leave qualifiers on</font>
<font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0; i < j; i++)
results.append(rs[i] + "\n");
}
<font color="#0000ff">class</font> StripL <font color="#0000ff">implements</font> ItemListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> itemStateChanged(ItemEvent e) {
reDisplay();
}
}
<font color="#0000ff">class</font> SearchForL <font color="#0000ff">implements</font> TextListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> textValueChanged(TextEvent e) {
reDisplay();
}
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
DisplayMethods applet = <font color="#0000ff">new</font> DisplayMethods();
Frame aFrame = <font color="#0000ff">new</font> Frame("Display Methods");
aFrame.addWindowListener(
<font color="#0000ff">new</font> WindowAdapter() {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(500,750);
applet.init();
applet.start();
aFrame.setVisible(<font color="#0000ff">true</font>);
}
}
<font color="#0000ff">class</font> StripQualifiers {
<font color="#0000ff">private</font> StreamTokenizer st;
<font color="#0000ff">public</font> StripQualifiers(String qualified) {
st = <font color="#0000ff">new</font> StreamTokenizer(
<font color="#0000ff">new</font> StringReader(qualified));
st.ordinaryChar(' ');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -