Invoke method in reflection getMethod method getClass () method, getmethodgetclass
Package com. swift; import java. util. *; import java. lang. reflect. *; public class ReflectDemo {public static void main (String [] args) throws Exception {ArrayList <String> list = new ArrayList <String> (); list. add ("enen"); for (String str: list) {System. out. println (str);} Class <? Extends ArrayList> clas = list. getClass (); ArrayList <String> li = (ArrayList <String>) clas. newInstance (); li. add ("san"); for (String str: li) {System. out. println (str);} Method [] metho = clas. getMethods (); for (Method met: metho) {System. out. println (met);} Method meth = clas. getMethod ("add", Object. class); //. class is the type class meth of the parameters of the add method. invoke (li, "123"); for (String str: li) {System. out. println (str);} Class <?> C = Class. forName ("com. swift. ADemo "); ADemo a = (ADemo) c. newInstance ();. fun (); Class cl =. getClass (); ADemo B = (ADemo) cl. newInstance (); B. fun (); Method met = cl. getMethod ("fun"); met. invoke (B); Method [] m = c. getDeclaredMethods (); for (Method me: m) {System. out. println (me);} Method method = c. getMethod ("fun"); method. invoke (a) ;}} class ADemo {public void fun () {System. out. println ("A demo. ");}}