資源簡介
代碼主要實現了Java反射應用實例, 包含利用反射方法創建對象(使用默認構造函數和自定義構造函數),訪問對應對象的方法(包括帶參數的和不帶參數的),訪問對應對象的域(Field). 從這里可以看到代碼的詳細解說:http://blog.csdn.net/hejiangtao

代碼片段和文件信息
/**
?*Author:?Jiangtao?He;?Email:?ross.jiangtao.he@gmail.com
?*File?Name:?MyReflect.java
?*Date:?2012-1-9
?*Copyright:?All?right?reserved?by?author?-?Jiangtao?He
?*Version:?MyJavaExpert?v1.0
?*/
package?com.ross.reflect;
import?java.lang.reflect.Constructor;
import?java.lang.reflect.Field;
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
import?java.util.ArrayList;
import?java.util.List;
/**
?*?Author:?Jiangtao?He;?Email:?ross.jiangtao.he@gmail.com
?*?Date:?2012-1-9
?*?Since:?MyJavaExpert?v1.0
?*?Description:?reflect?method?implementation?and?test
?*/
public?class?MyReflect
{
????/**
?????*?Author:?Jiangtao?He;?Email:?ross.jiangtao.he@gmail.com
?????*?Date:?2012-1-9?
?????*?Description:?Use?reflect?method?to?access?the?fields?and?methods?of?DataTypebean
?????*/
????public?static?void?main(String[]?args)?throws?ClassNotFoundException
????????????SecurityException?NoSuchMethodException?InstantiationException
????????????IllegalAccessException?IllegalArgumentException
????????????InvocationTargetException
????{
????????int?iInt?=?2012;
????????String?sStr?=?“This?a?string!“;
????????String[]?saStr?=?new?String[]?{?“First?item?of?the?string?array“
????????????????“Second?item?of?the?string?array“
????????????????“Third?item?of?the?string?array“?};
????????List?oList?=?new?ArrayList();
????????//?Initialize?the?oList
????????int?i?=?0;
????????while?(i?3)
????????{
????????????oList.add(i);
????????????i++;
????????}
????????//?get?class
????????Class?oClass?=?Class.forName(“com.ross.reflect.bean.DataTypeBean“);
????????//?get?the?toString?method?a?method?without?parameters
????????Method?oToStringMethod?=?oClass.getMethod(“toString“);
????????//?get?the?addDataToList?method?a?method?with?parameters
????????Method?oAddDataToListMethod?=?oClass.getMethod(“addDataToList“
????????????????int.class?int.class);
????????//?used?default?constructor?to?initialize?a?object
????????object?oDefalutobject?=?oClass.newInstance();
????????//?access?fields?process?getDeclaredFields?can?access?private?and
????????//?protected?fields
????????Field[]?oFields?=?oClass.getDeclaredFields();
????????for?(int?j?=?0;?j?????????{
????????????//?to?access?the?private
????????????oFields[j].setAccessible(true);
????????????//?getSimpleName?method?can?get?the?type?of?the?field?according?the
????????????//?field?type?set?the?data?to?the?field
????????????if?(“int“.equals(oFields[j].getType().getSimpleName()))
????????????{
????????????????oFields[j].setInt(oDefalutobject?iInt);
????????????}
????????????else?if?(“String[]“.equals(oFields[j].getType().getSimpleName()))
????????????{
????????????????oFields[j].set(oDefalutobject?saStr);
????????????}
????????????else?if?(“String“.equals(oFields[j].getType().getSimpleName()))
????????????{
????????????????oFields[j].set(oDefalutobject?sStr);
????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????310??2011-12-31?21:17??MyJavaExpert?V1.0\.classpath
?????文件????????393??2012-01-01?22:40??MyJavaExpert?V1.0\.project
?????文件??????22017??2011-12-30?00:58??MyJavaExpert?V1.0\.settings\org.eclipse.jdt.core.prefs
?????文件???????9884??2011-12-30?01:49??MyJavaExpert?V1.0\.settings\org.eclipse.jdt.ui.prefs
?????文件???????2868??2012-01-10?01:14??MyJavaExpert?V1.0\bin\com\ross\reflect\bean\DataTypeBean.class
?????文件???????3745??2012-01-10?01:14??MyJavaExpert?V1.0\bin\com\ross\reflect\MyReflect.class
?????文件???????2660??2012-01-10?01:14??MyJavaExpert?V1.0\src\com\ross\reflect\bean\DataTypeBean.java
?????文件???????4585??2012-01-10?01:14??MyJavaExpert?V1.0\src\com\ross\reflect\MyReflect.java
?????目錄??????????0??2012-01-10?00:15??MyJavaExpert?V1.0\bin\com\ross\reflect\bean
?????目錄??????????0??2012-01-10?00:15??MyJavaExpert?V1.0\src\com\ross\reflect\bean
?????目錄??????????0??2012-01-10?00:15??MyJavaExpert?V1.0\bin\com\ross\reflect
?????目錄??????????0??2012-01-10?00:15??MyJavaExpert?V1.0\src\com\ross\reflect
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\bin\com\ross
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\src\com\ross
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\bin\com
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\src\com
?????目錄??????????0??2011-12-29?23:37??MyJavaExpert?V1.0\.settings
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\bin
?????目錄??????????0??2012-01-04?23:27??MyJavaExpert?V1.0\src
?????目錄??????????0??2012-01-03?23:54??MyJavaExpert?V1.0
-----------?---------??----------?-----??----
????????????????46462????????????????????20
評論
共有 條評論