Package com.sanmao10;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Method;
public class Test_method {
/**
* Reflection (Fetch method via reflection and use)
* methods
* Class.getmethod (String, class ...) and Class.getdeclaredmethod (String, Class ...)
* method to get the specified method in the class, invoke invoke (object, Object ...) This method can be invoked, Class.getmethod ("Eat")
* Invoke (obj) Class.getmethod ("Eat", Int.class) Invoke (obj,10)/public
static void Main (string[] args) throws exception{
Class clazz=class.forname ("Com.sanmao10.Person");
Constructor Constructor=clazz.getconstructor (string.class,int.class);
Person p= (person) constructor.newinstance ("Sanmao");
Method M=clazz.getmethod ("Papapa"); Obtain the Papapa method
M.invoke (p);
Method M1=clazz.getmethod ("Papapa", String.class,int.class); Gets the Papapa method
M1.invoke (P, "Fan Bingbing",);
}