Study on Java reflection (1) 9. Call specific methods
Method M = c1.getmethod ("funcname", class <?>... C); // funcname indicates the name of the called method, and C indicates the Class Object of the parameter.
For example: method M = c1.getmethod ("fun", String. Class, Int. Class); Calls fun (string, INT); Function
Object OBJ = M. Invoke (c1.newinstance (), "xiazdong", 20); // if a return value exists, the invoke function returns;
Note: If you call a static method, you do not need to set the object;
Object OBJ = M. Invoke (null, "xiazdong ");
NOTE: If an array exists in the parameter, for example, public static void main (string [] ARGs );
Then
Method M = c1.getmethod ("Main", string []. Class );
M. Invoke (null,(Object)New String [] {"AA", "BB"}); yes;
M. Invoke (null, new string [] {"AA", "BB"}); Calls main (string, string); function;
10. Call specific properties
Field F = c1.getdeclaredfield ("name"); // The name attribute is returned.
F. setaccessible (true); // Private Attribute visible
String name = (string) F. Get (Object OBJ); // return the value of the name attribute of the OBJ object
F. Set (Object OBJ, string N); // you can specify N as the object name;
11. Operation Array
Int TMP [] = {1, 2, 3 };
Class <?> C = TMP. getclass (). getcomponenttype ();
Array. getlength (TMP); // The length of the TMP Array
C. getname (); // obtain the array type name
Array. Get (Object OBJ, int index); // obtain the index value of the OBJ Array
Array. Set (Object OBJ, int index, value); // set the index value of the OBJ array to value;
Object OBJ = array. newinstance (C, length); // C indicates the array type, and length indicates the length of the array. OBJ indicates the returned array object;
Example:
Import java. Lang. Reflect. *; public class getmethoddemo01 {public static void main (string ARGs []) throws exception {class <?> C1 = Class. forname ("person"); method M = c1.getmethod ("sayhello"); M. invoke (c1.newinstance (); Method m2 = c1.getmethod ("sayhello2", String. class, Int. class); string STR = (string) m2.invoke (c1.newinstance (), "xiazdong", 123); system. out. println (STR); field namefield = c1.getdeclaredfield ("name"); field agefield = c1.getdeclaredfield ("Age"); namefield. setaccessible (true); agefield. setaccessible (true); person OBJ = (Person) c1.newinstance (); obj. setname ("xzdong"); obj. setage (12); system. out. println (namefield. get (OBJ); system. out. println (agefield. get (OBJ); Method setname = c1.getmethod ("setname", String. class); setname. invoke (OBJ, "changed"); Method getname = c1.getmethod ("getname"); system. out. println (getname. invoke (OBJ); int TMP [] = {1, 2, 3}; Class <?> C3 = TMP. getclass (). getcomponenttype (); system. out. println (c3.getname (); system. out. println ("first number:" + array. get (TMP, 0); array. set (TMP, 0, 5); system. out. println ("first number:" + array. get (TMP, 0); object arr = array. newinstance (C3, 5); system. arraycopy (TMP, 0, arr, 0, array. getlength (TMP); system. out. println (array. get (ARR, 2); system. out. println (array. get (ARR, 3 ));}}
import java.lang.reflect.*;interface China{public static final String NAME = "CHINA";public int AGE = 60;public void sayHello();public String sayHello2(String name,int age);}class Person implements China{private String name;private int age;public String getName(){return name;}public void setName(String name){this.name = name;}public int getAge(){return age;}public void setAge(int age){this.age = age;}public void sayHello(){System.out.println(NAME+" "+AGE);}public String sayHello2(String name,int age){return name+" "+age;}}