測試類別
package testReflect;/** * @Package testReflect * @ClassName: Hello * @Description: TODO() * @author andy * @date 2013-5-31 上午09:59:29 */public class Hello { public String name; private int id = 1; public Hello(){ } public Hello(String name,int id){ this.name = name; this.id = id; } public String getUserName(){ return "123"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; }}
View Code
獲得某個類或對象的運行時Class的對象的三種方式
package testReflect;/** * @Package testReflect * @ClassName: InstancingClass * @Description: TODO(獲得某個類或對象的運行時Class的對象的三種方式) * @author andy * @date 2013-5-31 上午09:59:05 */public class InstancingClass<T> { public static void main(String[] args) throws ClassNotFoundException { //第一種 Class<Hello> classType1 = Hello.class; //第二種 Class<?> classType2 = Class.forName("testReflect.Hello"); //第三種 Hello hello = new Hello(); Class<? extends Hello> classType3 = hello.getClass(); System.out.println(classType1 + "\n" +classType2 + "\n" + classType3 + "\n"); System.out.println(classType1.getName() + "\n" +classType2.getName() + "\n" + classType3.getName() + "\n"); System.out.println(classType1.getSimpleName() + "\n" +classType2.getSimpleName() + "\n" + classType3.getSimpleName() + "\n"); }}
View Code
不用new關鍵字執行個體化對象的三種方法
package testReflect;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;/** * @Package testReflect * @ClassName: InstancingObject * @Description: TODO(不用new關鍵字執行個體化對象的三種方法) * @author andy * @date 2013-5-31 上午10:08:34 */public class InstancingObject { public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { Hello hello0 = Hello.class.newInstance(); Class<Hello> classType = Hello.class; //建構函式沒有參數時,第一種等價於第二種 //第一種 Hello hello1 = (Hello) classType.newInstance(); //第二種 Constructor<Hello> con = classType.getConstructor(); Hello hello2 = (Hello) con.newInstance(); //第三種 Constructor<Hello> con2 = classType.getConstructor(new Class[]{String.class, int.class}); Hello hello3 = (Hello)con2.newInstance(new Object[]{"123",123}); System.out.println(hello0.getUserName()); System.out.println(hello1.getUserName()); System.out.println(hello2.getUserName()); System.out.println(hello3.getUserName()); }}
View Code
通過反射對欄位的操作
package testReflect;import java.lang.reflect.AccessibleObject;import java.lang.reflect.Field;/** * @Package testReflect * @ClassName: AccessFieldByReflect * @Description: TODO(通過反射對欄位的操作) * @author andy * @date 2013-5-31 上午10:48:13 */public class AccessFieldByReflect { public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException { Hello hello = Hello.class.newInstance(); Field[] fields = hello.getClass().getDeclaredFields(); //遍曆擷取所有的欄位 for(Field field : fields){ //System.out.println(field); System.out.println(field.getName()); } System.out.println("沒有修改前:" + hello.getName()); System.out.println("沒有修改前:" + hello.getId()); for(Field field : fields){ if("id".equals(field.getName())){ //允許訪問所有Field、Method 和 Constructor 私人元素 field.setAccessible(true); //指定只允許訪問的Field AccessibleObject[] accessParam = {field}; Field.setAccessible(accessParam,true); //如果那個欄位是int就setint,是double就setdouble field.setInt(hello, 123); }else{ //欄位是字串 field.set(hello, "張三"); } } System.out.println("通過反射修改後:" + hello.getId()); System.out.println("通過反射修改後:" + hello.getName()); }}
View Code
通過反射對方法的操作
package testReflect;import java.lang.reflect.AccessibleObject;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/** * @Package testReflect * @ClassName: AccessMethodByReflect * @Description: TODO(通過反射對方法的操作) * @author andy * @date 2013-5-31 上午11:25:39 */public class AccessMethodByReflect { public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { Hello hello = Hello.class.newInstance(); Method[] methods = hello.getClass().getDeclaredMethods(); //遍曆擷取所有的欄位 for(Method method : methods){ //System.out.println(field); System.out.println(method.getName()); } System.out.println("沒有修改前:" + hello.getName()); System.out.println("沒有修改前:" + hello.getId()); for(Method method : methods){ if("setId".equals(method.getName())){ //允許訪問所有Field、Method 和 Constructor 私人元素 method.setAccessible(true); //只允許訪問指定的Method AccessibleObject[] accessParam = {method}; Method.setAccessible(accessParam,true); }else if("setName".equals(method.getName())){ method.invoke(hello, "張三"); } } String obj = (String) hello.getClass().getDeclaredMethod("getName").invoke(hello); System.out.println("通過反射getName方法返回來的值:" + obj); System.out.println("通過反射修改後:" + hello.getId()); System.out.println("通過反射修改後:" + hello.getName()); }}
View Code
Integer.TYPE 返回的int,即返回的是它們所對應的原生資料class對象(所有的封裝類都是這樣的),
Integer.class 返回的是它所對應的Class對象 java.lang.Integer