Methods in the 1:class class
Getdeclaredmethod (String name, Class<?> parametertypes) throws Nosuchmethodexception, SecurityException
Number of references:
name -Method name
parameterTypes -array of parameters
Return:
The object of the method that matches the specified name and the number of parameters of the class Method
Methods in the 2:method class;
Invoke (Object obj, Object ... args) throws Illegalaccessexception, illegalargumentexception, invocationtargetexception
Number of references:
obj -Object from which the underlying method is called
args -The number of parameters used for the method call
Return:
Use a number of parameters argsIn objThe result of assigning the method represented by the object 3:hello the reflection call at the world! level:
package Com.dao.text;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;public Class Tetsreflect {/** * @param args * @throws nosuchmethodexception * @throws SecurityException * @throws invocationtargetexcep tion * @throws illegalaccessexception * @throws illegalargumentexception */public static void Main (string[] args) throws S Ecurityexception,nosuchmethodexception, Illegalargumentexception,illegalaccessexception, invocationtargetexception {Person p = new person (); Class CLA = P.getclass ();//1: No reference method method1 = Cla.getdeclaredmethod ("print"), Method1.invoke (P);//2: Call method with a number of parameters METHOD2 = Cla.getdeclaredmethod ("Printparameter", String.class); Method2.invoke (P, "Hello world!!");}} class Person {public void print () {System.out.println ("Hello world!!" + "Call without parameter Reflection method");} public void Printparameter (String param) {System.out.println (param+ "Call to the reflection method with a reference");}}
Results:
Hello world!! Call without a parameter reflection method
Hello world!! Invocation of a reflection method with a number of parameters
Java Reflection Call Method