Java 27-5 Reflection Gets the member method by reflection and uses the

Source: Internet
Author: User

Class method: Provides information about a single method (and how to access the method) on a class or interface.

 

A: Get all method arrays

1. GetMethods gets the public member method of the class including its parent class

2. Getdeclaredmethods gets all the member methods of the class

B: Get a single method

1. GetMethod get all public class single member methods for this class

2. Getdeclaredmethod gets a single member method for all types of this class

A.1: Gets the public member method of the class including its parent class

        // Get bytecode files                Class C = class.forname ("Zl_relfect_01.person");                 // get all public member methods        of the class and the parent class Method[] M1 = c.getmethods ();          for (Method m:m1) {            System.out.println (m);        }

A.2:getdeclaredmethods gets all the member methods of the class

1         // Get bytecode files         2         Class C = class.forname ("Zl_relfect_01.person"); 3         // gets the member method for all types of the class 4         Method[] M1 = c.getdeclaredmethods (); 5          for (Method m:m1) {6            System.out.println (m); 7         }

B: Get a single member method:

1         //Get bytecode files       2Class C = class.forname ("Zl_relfect_01.person");3         // No parameter Construction create object 4Constructor con =c.getconstructor ();5Object obj =con.newinstance ();6         7          // Gets the member method of a public type with a single, no parameter return value for the class 8         //no parameter member method, just write the name of the member method9Method m1 = C.getmethod ("Show");Ten         //Public object Invoke (Object Obj,object ... args) The return value is Object received, the first parameter represents who the object is, and the second parameter represents the actual argument that called the method One         //Show is no parameter, no return value constructed AM1.invoke (obj);//calling the show method of the person class -          -          the          // Gets the Class A member method that has a single parameter that has a public type of return value  -Method m2 = C.getmethod ("Method", String.class); -          // no return value  -M2.invoke (obj, "hello");////call method of the person class +          -          // Gets the Class A member method that has a single parameter that has a public type of return value  +Method m3 = C.getmethod ("getString", String.class,int.class); A         // There is a return value, public object Invoke (Object Obj,object ... args) The return value is Object receive atObject ob1 = M3.invoke (obj, "Hello", 123); - System.out.println (OB1); -          -          // Gets a private member method that has no return value for the class single parameter  -Method M4 = C.getdeclaredmethod ("function"); -          // Cancel language access check  inM4.setaccessible (true); -         //call the function method of the person class toM4.invoke (obj);

Java 27-5 Reflection Gets the member method by reflection and uses the

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.