Use of invoke in Java

Source: Internet
Author: User

ImportJava. Lang. Reflect .*;
Public class testm
{

Public static void main (string [] ARGs)
{

Try {

Testm T = new testm ();
Class C = T. getclass ();
Class [] cargs = new class [2];
String [] realargs = {"AA", "BB "};
Cargs [0] = realargs. getclass ();
Integer in = new INTEGER (2 );
Cargs [1] = in. getclass ();
Method M = C. getmethod ("test", cargs); // The test method is called and the return value is the method variable.
Object [] inargs = new object [2];
Inargs [0] = readargs; // Ha, here the error of wingzhang should be realargs.
Inargs [1] = in;
M.Invoke(T, inargs );

} Catch (exception e) {system. Out. println (E );}

}

Public void test (string [] STR, integer stri)
{

For (Int J = 0; j <Str. length; j ++)
System. Out. println (STR [J]);
System. Out. println (stri. intvalue ());

}

}

//
Invoke(Object proxy, method, object [] ARGs)
Process method calls on the proxy instance and return results.
//
Proxy-proxy instance on which the method is called
Method-the Method Instance corresponding to the interface method called on the proxy instance. The method object Declaration class will be the interface in which the method is declared. This interface can be a superinterface of the proxy interface on which the proxy class inherits the method.
ARGs-an array of objects that contain the parameter values of method calls on the proxy instance. If the interface method does not use a parameter, It is null. Parameters of the basic type are encapsulated in the appropriate basic package class (suchJava. Lang. Integer orJava. Lang. Boolean.

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.