package lc.abin.lee.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class MyMethod {
private static String name;
public MyMethod() {
}
public MyMethod(String myname) {
this.name=myname;
}
public static String getName(){
return name;
}
public static String getMessage(){
return "北京欢迎您";
}
public static String result(String message){
return message.replace("my", "abin");
}
public static int status(int enter,int end){
return end+enter;
}
public static void main(String[] args) throws Exception {
MyMethod my=new MyMethod();
Class<?> cls=my.getClass();
Method mes=cls.getDeclaredMethod("getMessage");
String message=(String)mes.invoke(my);
System.out.println("message="+message);
Method mld=cls.getDeclaredMethod("result", String.class);
String result=(String)mld.invoke(my,"myarea");
System.out.println("result="+result);
Method plus=cls.getDeclaredMethod("status",int.class,int.class);
int status=(Integer)plus.invoke(my,5,195);
System.out.println("status="+status);
Class<?>[] cl={String.class};
Constructor<?> cul=cls.getConstructor(cl);
Object obj=cul.newInstance("abin1");
System.out.println("obj="+obj.getClass());
Method nmd=cls.getDeclaredMethod("getName");
String gong=(String)nmd.invoke(cl);
System.out.println("gong="+gong);
}
}