你这个问题很抽象,这个里面有Field的根据字段并修改,你自己建立一个Person类,- public class JavaBeanDemo {
- public static void main(String[] args) throws Exception{
- //Person p = new Person();
- Class<?> c = Class.forName("temp.Person");
- //通过字节码兑现创建实例对象
- Field f = c.getDeclaredField("age");
- Object obj = c.newInstance();//创建对象
- f.setAccessible(true);
- f.set(obj, 24);//调方法传参
-
- /*Method[] m1 = c.getDeclaredMethods();
- for(Method me:m1){
- System.out.println(me);
- }*/
- Method m = c.getDeclaredMethod("setName",String.class);
- m.invoke(obj, "伯爵");
-
- System.out.println(obj);
- }
-
- }
复制代码 |