/** * MethodName: getReflection <br> * คำอธิบาย: respxml 在通过反射设置对象属性值 * ผู้ใช้: liqijing * วันที่: 2015-7-19 下午 12:42:55 * @param clzzname * @param respxml * @return InstantiationException * @throws SecurityException * @throws nosuchfieldexception */สาธารณะคงที่ <t> วัตถุ getReflection (สตริง clzzname, สตริง respxml) พ่น classnotfoundexception, documentexception, asledalargumentException clz = class.forname (clzzname) .newinstance (). getClass (); เอกสารเอกสาร = null; doc = documenthelper.parsetext (respxml); Element el = doc.getRootelement (); สำหรับ (field f: clz.getDeclaredfields () (องค์ประกอบ) it.next (); ถ้า (f.getName (). เท่ากับ (elt.getName ())) {f = clz.getDeclaredField (elt.getName ()); f.setAccessible (true); f.set (o, elt.getText ();ถึงทุกคน:
在开发过程中有类似的需求通过反射动态设置属性值, 希望有帮助。也很高兴与大家分享, 谢谢。
以上这篇 Java 通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。