Java reflection is Shared using examples
- 2020-04-01 02:58:48
- OfStack
public class ReflexTest {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException,
InstantiationException {
//The parameter types
Class[] paramTypes = new Class[3];
paramTypes[0] = String.class;
paramTypes[1] = Integer.class;
paramTypes[2] = String.class;
//Obtain method
Method m = Demo.class.getDeclaredMethod("getSome", paramTypes);
//Parameter Settings
Object[] os = new Object[3];
os[0] = "pp";
os[1] = 4;
os[2] = "3";
//The method call
m.invoke(Demo.class.newInstance(), os);
}
}
class Demo{
public void getSome(String name,Integer year, String age){
System.out.println("name is :" + name + ";age is :" + age + ";i is :" + year);
}
}