国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java反射的理解

2019-11-15 00:25:55
字體:
供稿:網(wǎng)友
java反射的理解 Posted on 2015-06-02 22:41 Algori 閱讀(...) 評論(...) 編輯 收藏反射的作用:1.運行時檢查類的結(jié)構(gòu)2.運行時更改類的字段值3.調(diào)用類的方法準(zhǔn)備知識:Class類:虛擬機(jī)為每一個對象保存的一份對象所屬類的清單:static Class forName(String className) 獲取字符串(接口或者類的全類名)對應(yīng)的類的Class對象。Object newInstance() 返回Class對應(yīng)的類的一個對象解析:1.運行時檢查類的結(jié)構(gòu) java.lang.reflection 中有三個類: 1.Field 對應(yīng)類的字段 getName() 返回Field對應(yīng)的名稱 getType() 返回Field所在的Class類型 2.Method 對應(yīng)類的方法 getName() 返回Method對應(yīng)的名稱 Class<?>[] getParameterTypes() 按方法聲明的順序返回參數(shù)類型數(shù)組 int getModifiers() 返回一個整數(shù)值,用不同的位開關(guān)表示static,public這樣的修飾情況。 3.Constructor 對應(yīng)構(gòu)造器 getName() 返回Constructor對應(yīng)的名稱 Class<?>[] getParameterTypes() 按方法聲明的順序返回參數(shù)類型數(shù)組 int getModifiers() 返回一個整數(shù)值,用不同的位開關(guān)表示static,public這樣的修飾情況。在Method和Constructor中可以使用Modifier類的isPRivate,isStatic 來判斷getModifiers()的返回值,給出是否含有對應(yīng)的修飾符。Class對象的getDeclaredConstructors,getDeclaredMethods,getDeclaredFields分別用于獲取對象的構(gòu)造器,方法,字段,以數(shù)組的形式返回。下面為反射基本用法:檢測一個類的結(jié)構(gòu):
 1 import java.lang.reflect.Constructor; 2 import java.lang.reflect.Field; 3 import java.lang.reflect.Method; 4 import java.lang.reflect.Modifier; 5  6 //反射基本測試 7 public class ReflectionTest { 8     public static void main(String[] args) { 9         String name = "java.util.Date";10         try {11             Class cl = Class.forName(name);12             Class supercl = cl.getSuperclass();13             String modifiers = Modifier.toString(cl.getModifiers());14             if (modifiers.length() > 0) System.out.print(modifiers + " ");15             System.out.print("class " + name);16             if (supercl != null && supercl != Object.class) System.out.print(" extends "17                     + supercl.getName());18 19             System.out.print("/n{/n");20             printConstructors(cl);21             System.out.println();22             printMethods(cl);23             System.out.println();24             printFields(cl);25             System.out.println("}");26         } catch (ClassNotFoundException e) {27             e.printStackTrace();28         }29         System.exit(0);30     }31 32 33     public static void printConstructors(Class cl) {34         Constructor[] constructors = cl.getDeclaredConstructors();35 36         for (Constructor c : constructors) {37             String name = c.getName();38             System.out.print("   ");39             String modifiers = Modifier.toString(c.getModifiers());40             if (modifiers.length() > 0) System.out.print(modifiers + " ");41             System.out.print(name + "(");42 43             // print parameter types44             Class[] paramTypes = c.getParameterTypes();45             for (int j = 0; j < paramTypes.length; j++) {46                 if (j > 0) System.out.print(", ");47                 System.out.print(paramTypes[j].getName());48             }49             System.out.println(");");50         }51     }52 53 54     public static void printMethods(Class cl) {55         Method[] methods = cl.getDeclaredMethods();56 57         for (Method m : methods) {58 59             Class retType = m.getReturnType();60             String name = m.getName();61 62             System.out.print("   ");63             // print modifiers, return type and method name64             String modifiers = Modifier.toString(m.getModifiers());65             if (modifiers.length() > 0) System.out.print(modifiers + " ");66             System.out.print(retType.getName() + " " + name + "(");67 68             // print parameter types69             Class[] paramTypes = m.getParameterTypes();70             for (int j = 0; j < paramTypes.length; j++) {71                 if (j > 0) System.out.print(", ");72                 System.out.print(paramTypes[j].getName());73             }74             System.out.println(");");75         }76     }77 78 79     public static void printFields(Class cl) {80         Field[] fields = cl.getDeclaredFields();81 82         for (Field f : fields) {83             Class type = f.getType();84             String name = f.getName();85             System.out.print("   ");86             String modifiers = Modifier.toString(f.getModifiers());87             if (modifiers.length() > 0) System.out.print(modifiers + " ");88             System.out.println(type.getName() + " " + name + ";");89         }90     }91 }

2.更改類的字段值: Field對象有以下對應(yīng)的一系列方法: public Object get(Object obj) 獲取目標(biāo)對象上字段的值 public void set(Object obj, Object value)設(shè)置目標(biāo)對象上字段的值 有相應(yīng)具體類型的get和set方法。3.調(diào)用類的方法(類似于方法指針): Method對象方法: public Object invoke(Object obj, Object... args) obj是調(diào)用的目標(biāo)對象,ars是方法參數(shù)以下為示例代碼:
 1 import java.lang.reflect.Field; 2 import java.lang.reflect.InvocationTargetException; 3 import java.lang.reflect.Method; 4  5 /** 6  * Created by karlx on 2015/5/29. 7  */ 8 public class ReflectionTest2 { 9     public static void main(String[] args) {10         Person person = new Person();11         person.name = "karl";12         //獲取,設(shè)置 運行中對象的字段值13         Class clazz = person.getClass();14         try {15             Field field = clazz.getField("name");16             field.setaccessible(true);//避開java的訪問檢查17             System.out.println(field.get(person));18 19             field.set(person, "xiaoming");20             System.out.println(field.get(person));21 22         } catch (NoSuchFieldException | IllegalAccessException e) {23             e.printStackTrace();24         }25         //運行中調(diào)用對象的方法26         try {27             Method method = clazz.getMethod("getName");28             method.setAccessible(true);//避開java的訪問檢查29             System.out.println(method.invoke(person));30         } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {31             e.printStackTrace();32         }33     }34 35     static class Person {36         public String name;37 38         public String getName() {39             return name + "hello";40         }41 42         public void setName(String name) {43             this.name = name;44         }45     }46 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 拉萨市| 博白县| 漾濞| 印江| 贵南县| 台山市| 阿克陶县| 富蕴县| 敖汉旗| 大庆市| 嵊泗县| 阳山县| 沐川县| 惠水县| 涿州市| 商洛市| 梁平县| 桐柏县| 永吉县| 彰化市| 双流县| 兴安盟| 白玉县| 罗源县| 腾冲县| 永仁县| 古交市| 文登市| 交城县| 北流市| 维西| 玉门市| 大洼县| 沐川县| 孝昌县| 垦利县| 哈巴河县| 桓台县| 钟祥市| 乌恰县| 许昌县|