In method Invoke (Object Obj,object...args) The first argument is an instance of the class, the second argument is the parameter in the corresponding function, I would like to ask, I called the function is a multi-parameter (parameter number of uncertain) function, what should do.
This can be called: Method.invoke (object, New Object[][]{new object[]{obj1, obj2});
This is equivalent to Object.Method (Obj1, OBJ2);
Take a look at the following example:
Import Java.lang.reflect.Array;
Import Java.lang.reflect.Method;
public class Test
{public
void print (string string)
{
System.out.println ("Print (string)");
System.out.println (string);
public void print (string ... strings)
{
System.out.println ("Print (string. ... string)");
for (string string:strings)
System.out.println (string);
public static void Main (string[] args) throws Exception
{
test test = new test ();
Call print (String string) method
method1 = Test.getclass (). GetMethod ("print", string.class);
Method1.invoke (Test, "a");
Call Print (String ... strings) method
method2 = Test.getclass (). GetMethod ("Print", Array.newinstance ( String.class, 0). GetClass ());
Method2.invoke (test, new String[][]{new string[]{"A", "B"}});
}