Java method Invoke Parameter problem

Source: Internet
Author: User
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"}});
    }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.