Reprint Please specify : Http://blog.csdn.net/uniquewonderq
1. How to get a method
The name of the method and the parameter list of the method can only determine a method
2. Reflection operation of the method
Method.invoke (object, parameter list)
1. Get a method that gets the information of the class, gets the information of the class, and first gets the class type of the class .
2. Get the method name and the parameter list to determine
GetMethod gets the public method
Getdeclaredmethod How to get all the claims
<span style= "FONT-SIZE:18PX;" >package Com.test;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.Method;import Java.util.logging.level;import java.util.logging.logger;/** * * @author WONDERQ */public class Methodreflect {public S tatic void Main (string[] args) {//Gets the print method, but there are two, which gets the print (int, int)//1 here. Getting a method is to get the information of the class, get the information of the class, first get the class type of the class 。 A a1=new a (); Class C=a1.getclass (); try {/* 2. Gets the method name and the argument list to determine GetMethod gets the public method Getdeclaredmethod gets all the declared methods */Method M=c.getmethod ("print", int.class,int.class);//The second parameter is the reflection operation of the variable parameter//approach; A1.print (10, 20); The reflection operation of a method is the same as the effect of a method call and a A1.print call with an M object: if there is no return value, NULL is returned, and the return value returns a specific return value//object o =m.invoke (A1, New object[]{10,20}); Object O=m.invoke (A1, 10,20); System.out.println ("========================="); Gets the method print (string,string) object MetHod M1=c.getmethod ("print", string.class,string.class); Perform a reflection operation on the method. A1.print ("Hello", "World"); O=m1.invoke (A1, "Hello", "World");//System.out.println ("=========================") in accordance with the above effects; Method M2=c.getmethod ("print");//This time the second parameter, no, then do not pass, the variable parameter can be empty m2.invoke (A1); } catch (Nosuchmethodexception ex) {ex.printstacktrace (); } catch (SecurityException ex) {ex.printstacktrace (); } catch (Illegalaccessexception ex) {Logger.getlogger (MethodReflect.class.getName ()). log (Level.severe, NULL, E x); } catch (IllegalArgumentException ex) {Logger.getlogger (MethodReflect.class.getName ()). log (Level.severe, NULL, ex); } catch (InvocationTargetException ex) {Logger.getlogger (MethodReflect.class.getName ()). log (Level.severe, NULL , ex); }}}class a{public void print () {System.out.println ("helloworld!"); } public void PRINT (int a,int b) {System.out.println ("a+b=" + (a+b)); public void print (String a,string b) {System.out.println (a.touppercase () + "," +b.tolowercase ()); }}</span>
Output Result:
Run
A+b=30
=========================
Hello,world
=========================
helloworld!
This is the Get method object and then the reflection operation with the method object.
Java--reflect (reflection) Thematic 5--method The basic operation of reflection