方法
|
說明
|
Constructor getConstructor(Class[] params)
|
獲得使用特殊的參數類型的
公共構造函數
|
Constructor[] getConstructors()
|
獲得類的所有公共構造函數
|
Constructor getDeclaredConstructor(Class[] params)
|
獲得使用特定參數類型的構造函數(與訪問級別無關)
|
Constructor[] getDeclaredConstructors()
|
獲得類的所有構造函數(與訪問級別無關)
|
方法
|
說明
|
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()
|
獲得類聲明的所有方法
|
Class c = Class.forName("java.lang.String");
Method m[] = c.getDeclaredMethods();
System.out.println(m[0].toString());
|
class DemoReflection
{
private String name = null;
private void doPrint() {
System.out.println("print.....");
}
};
|
package cn.softworks.demo;
import java.lang.reflect.*;
publicclass TestReflection {
publicstaticvoid main(String args[]) {
try {
// 通過反映射技術得到DemoReflection的類型
Class cls = Class.forName("DemoReflection");
// 動態創建DemoReflection類的實力
Object instance = cls.newInstance();
// 通過反映射技術得到DemoReflection的非公有方法doPrint
Method m = cls.getDeclaredMethod("doPrint",
new Class[] { String.class });
// 表示可以隨意訪問該類中的方法
m.setAccessible(true);
// 調用doPrint方法
m.invoke(instance, new Object[] { "Softworks" });
} catch (Exception ex) {
System.out.println(ex);
}
}
};
|
Cleaner clearner = new Cleaner(“Chen.yu”);
Broom broom = new Broom();
cleaner.clear(broom);
|
package cn.softworks.test;
import cn.softworks.demo.BeanFactory;
import cn.softworks.demo.Cleaner;
import cn.softworks.demo.IClearEquipment;
/**
*反射機制的測試類
*
*@version1.0
*
*@authorChen.yu
*
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicclass TestClient {
publicstaticvoid main(String args[]) {
//通過工廠創建指定的清潔工具類
IClearEquipment eq =
(IClearEquipment) BeanFactory.newInstance().creator("ClearEquipment");
if(eq == null)
return;
//創建清潔工對象
Cleaner cleaner = new Cleaner();
//清潔工人開始清潔
cleaner.clear(eq);
}
}
|
以下是IClearEquipment接口的代碼片斷:
package cn.softworks.demo;
/**
*
*該接口是的作用是用來定義清潔設備的標準<br>
*換句話說,如果要想成爲清潔設備,那麼就必須要具有清潔能力
*
*@version1.0
*
*@authorChen.yu
*
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicinterface IClearEquipment {
/**
*清潔設備的清潔方法<br>
*不同的清潔設備有不同的清潔方法
*
*/
publicvoid clear();
}
|
以下是BeanFactory類的代碼片斷:
package cn.softworks.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*該類的作用是從配置文件中讀取類名,並依靠反射將指定類的實體<br>
*返回,以此達到“清潔工”類和“清潔設備”類之間的解耦<br>
*
*該類被設置成了單例模式,並在創建指定類的時候加入了同步鎖,<br>
*以此保證線程安全。
*
*@version1.0
*
*@authorChen.yu
*
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicclass BeanFactory {
/**
*單例工廠實體
*/
privatestatic BeanFactory instance = null;
/**
*用於保存softworks.cfg.properties配置文件的實體。
*/
privatestatic Properties config = null;
/**
*默認配置文件路徑
*/
privatestaticfinal String CONFIG_PATH = "softworks.cfg.properties";
|
/**
*使用了單例模式的工廠類默認構造函數
*
*/
private BeanFactory() {
//得到配置文件的路徑
InputStream stream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(CONFIG_PATH);
config = new Properties();
try {
//將配置文件信息加載到config對象中
config.load(stream);
} catch (IOException e) {
instance = null;
}
}
/**
*創建BeanFactory實體的靜態方法
*
*@returnBeanFactory的單例實體
*/
publicsynchronizedstatic BeanFactory newInstance() {
//判斷BeanFactory的實體是否已經存在
if (instance != null)
returninstance;
//如果BeanFactory實體不存在那麼立刻創建
instance = new BeanFactory();
returninstance;
}
|
/**
*工廠了的創建方法,該方法可以用於創建配置文件中指定key名的類<br>
*現在配置文件中有如下信息:<br>
*ClearEquipment=cn.softworks.demo.DustCollector<br>
*那麼當參數被設置成ClearEquipment的時候,通過該方法就會創建<br>
*cn.softworks.demo.DustCollector類的實體。
*
*@paramkey 配置文件中類所對應的Key名
*
*@return 被加載類的實體
*/
publicsynchronized Object creator(String key) {
if(config == null)
returnnull;
//得到配置文件中的類名
String className = config.getProperty(key);
try {
//通過反射機制創建類實體
return Class.forName(className).newInstance();
} catch (Exception e) {
returnnull;
}
}
}
|
以下是Broom類的代碼片斷:
package cn.softworks.demo;
/**
*該類是用來描述掃帚這個清潔工具的<br>
*它實現了清潔工具接口,所以必須實現清潔方法
*@version1.0
*@authorChen.yu
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicclass Broom implements IClearEquipment {
/**
*掃帚的清潔方法
*/
publicvoid clear() {
System.out.println("The Cleaner Use Broom");
}
}
|
package cn.softworks.demo;
/**
*該類是用來描述吸塵器這個清潔工具的<br>
*它實現了清潔工具接口,所以必須實現清潔方法
*@version1.0
*@authorChen.yu
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicclass DustCollector implements IClearEquipment {
/**
*掃帚的清潔方法
*/
publicvoid clear() {
System.out.println("The Cleaner Use Dust Collector");
}
}
|
以下是Cleaner類的代碼片斷:
package cn.softworks.demo;
/**
*該類是用來描述一個清潔工人的<br>
*清潔工人會使用清潔設備來進行清潔工作的
*
*@version1.0
*
*@authorChen.yu
*
*上海Softworks對日軟件人才培訓中心版權所有
*/
publicclass Cleaner {
/**
*這個方法的作用是定義清潔工人的清潔行爲<br>
*可以肯定的是,清潔工人必須藉助清潔設備才能清潔
*
*@parameq使用的清潔設備
*/
publicvoid clear(IClearEquipment eq) {
//清潔工人使用清潔設備進行清潔
eq.clear();
}
}
|