//0.4 Class.forName 利用泛型和数组, 任意调用Class/method
//牛掰的代码
try {
Class c21 = Class.forName(className);
Integer iclassParamCnt=3;
Class<?>[] classParam = new Class[iclassParamCnt]; //为毛要用数组?好问题,请仔细看此处Sun的Class的代码
classParam[0] = String.class;
classParam[1] = int.class;
classParam[2] = String.class;
Constructor con1c21 = c21.getConstructor(classParam);
Integer iInstanceParamCnt=3;
Object[] InstanceParm = new Object[iInstanceParamCnt];
InstanceParm[0] = "小明111";
InstanceParm[1] = 222;
InstanceParm[2] = "哈尔滨111";
Object obj1c21 = con1c21.newInstance(InstanceParm);
Integer iMethodParamCnt = 1;
Class<?>[] methodParam = new Class[iMethodParamCnt];
methodParam[0] = String.class;
String MethodName="method4";
iMethodParamCnt = 1;
Class[] parameterTypes = new Class[iMethodParamCnt];
parameterTypes[0] = String.class;
Object[] methodArgs = new Object[iMethodParamCnt];
methodArgs[0] = "itcast";
Method method4obj1c21 = c21.getMethod(MethodName, parameterTypes);
Object str21 = (String)method4obj1c21.invoke(obj1c21, methodArgs);
System.out.println("str21:" + str21);
} catch (Exception e) {
e.printStackTrace();
}
利用泛型、数组和Reflection , 任意调用Class/method
最新推荐文章于 2024-06-14 15:02:13 发布