基本學習:
Constructor getConstructor(Class[] params) -- 獲得使用特殊的參數類型的公共構造函數,
Constructor[] getConstructors() -- 獲得類的所有公共構造函數
Constructor getDeclaredConstructor(Class[] params) -- 獲得使用特定參數類型的構造函數(與接入級別無關)
Constructor[] getDeclaredConstructors() -- 獲得類的所有構造函數(與接入級別無關)
獲得字段信息的Class 反射調用不同于那些用于接入構造函數的調用,在參數類型數組中使用了字段名:
Field getField(String name) -- 獲得命名的公共字段
Field[] getFields() -- 獲得類的所有公共字段
Field getDeclaredField(String name) -- 獲得類聲明的命名的字段
Field[] getDeclaredFields() -- 獲得類聲明的所有字段
用于獲得方法信息函數:
Method getMethod(String name, Class[] params) -- 使用特定的參數類型,獲得命名的公共方法
Method[] getMethods() -- 獲得類的所有公共方法
Method getDeclaredMethod(String name, Class[] params) -- 使用特寫的參數類型,獲得類聲明的命名的方法
Method[] getDeclaredMethods() -- 獲得類聲明的所有方法
import java.awt.Button;
import java.lang.reflect.Method;
import java.util.Hashtable;


/** *//** *//** *//**
* 測試Java的反射機制
* @author Amigo Xie(xiexingxing1121@126.com)
* @since 2007/04/20 21:40
*/

public class ReflectionTest
{


/** *//** *//** *//**
* @param args
*/

public static void main(String[] args) throws Exception
{
ReflectionTest reflection = new ReflectionTest();
reflection.getNameTest();
System.out.println("");
reflection.getMethodTest();
}

/** *//** *//** *//**
* Class的getName()方法測試
* @throws Exception
*/

public void getNameTest() throws Exception
{
System.out.println("===========begin getNameTest============");
String name = "amigo";
Class cls = name.getClass();
System.out.println("string class name: " + cls.getName());
Button btn = new Button();
Class btnClass = btn.getClass();
System.out.println("button class name: " + btnClass.getName());
Class superBtnClass = btnClass.getSuperclass();
System.out.println("super button class name: " + superBtnClass.getName());
Class clsTest = Class.forName("java.awt.Button");
System.out.println("clsTest name: " + clsTest.getName());
System.out.println("===========end getNameTest============");
}

/** *//** *//** *//**
* Class的getMethod()方法測試
* @throws Exception
*/

public void getMethodTest() throws Exception
{
System.out.println("===========begin getMethodTest==========");
Class cls = Class.forName("ReflectionTest");
Class ptypes[] = new Class[2];
ptypes[0] = Class.forName("java.lang.String");
ptypes[1] = Class.forName("java.util.Hashtable");
Method method = cls.getMethod("testMethod", ptypes);
Object args[] = new Object[2];
args[0] = "hello, my dear!";
Hashtable ht = new Hashtable();
ht.put("name", "amigo");
args[1] = ht;
String returnStr = (String) method.invoke(new ReflectionTest(), args);
System.out.println("returnStr= " + returnStr);
System.out.println("===========end getMethodTest==========");
}


public String testMethod(String str, Hashtable ht) throws Exception
{
String returnStr = "return str";
System.out.println("begin testMethod invoked");
System.out.println("str= " + str);
System.out.println("name= " + (String) ht.get("name"));
System.out.println("end testMethod invoked");
return returnStr;
}
}

控制臺輸出如下:
===========begin getNameTest============
string class name: java.lang.String
button class name: java.awt.Button
super button class name: java.awt.Component
clsTest name: java.awt.Button
===========end getNameTest============
===========begin getMethodTest==========
begin testMethod invoked......
str= hello, my dear!
name= amigo
end testMethod invoked......
returnStr= return str
===========end getMethodTest==========