/** * MethodName: getReflection <br> * Deskripsi : 解析 respxml 在通过反射设置对象属性值 * Pengguna: liqijing * Tanggal : 2015-7-19 下午 12:42:55 * @param clzzname * @param respxml * @trowsnowscepsion * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throwsception * @throw IllegalAccessException * @Throws InstantiationException * @throws SecurityException * @throws NosuchfieldException */Public Static <T> GetReflection (String ClzzName, String RespXML) Melempar ClassNotFoundException, IllegalArgumentException, IllegalExceptionCceptionCceptionCceptionCceptionCceptioncception, IllegalArgumentException, IllegalExceptioncceptioncceptioncceptioncceptionceptionceptionception, IllegalArgumentExceptionException, NosuchfieldException {objek o = class.forname (clzzname) .newInstance (); class clz = class.forname (clzzname) .newInstance (). GetSclass (); dokumen doc = null; documenthelper.parsetext (respxml); elemen el = doc.getrootelement = documenthelper.parsetext (respxml); elemen el = doc.getrootelement = documenthelper.parsetext (respxml); elemen el = doc.getrootelement = documenthelper.parsetext (respxml); elemen el = doc.getrootelement. clz.getDecLaredFields ()) {iterator it = el.elementIterator (); while (it.hasnext ()) {elemen elt = (element) it.next (); if (f.getName (). Equals (elt.getName ())) {f = clz.getDeclaredfield (elt.getName ()); f.setAccessible (true); f.set (o, elt.getText ());}}} return o;}Halo semuanya:
在开发过程中有类似的需求通过反射动态设置属性值 , 希望有帮助。也很高兴与大家分享 , 谢谢。
以上这篇 Java 通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , ,