看你传入什么参数,如果是类名则:
Object array = java.lang.reflect.Array.newInstance(Class.forName(clzName), 10);
如果是对象则
Object array = java.lang.reflect.Array.newInstance(obj.getClass……
给你一个person的例子
public class Test{
public static void main(String[] args) throws Exception{
Class c = Class.forName("Test.Persion");
Constructor constructor= c.getConstructor();
Persion person1 =(Persion)constructor.newInstance();
Method mothod1= person1.getClass().getMethod("setAge",int.class);
mothod1.invoke(person1, 35);
Method mothod2= person1.getClass().getMethod("setName",String.class );
mothod2.invoke(person1, "lzhongyu");
Field m1= person1.getClass().getField("age");
System.out.println(m1.get(person1));
Field m2= person1.getClass().getField("name");
System.out.println(m2.get(person1));
}
} |