public class InvokeTester
{
public int add(int param1 ,int param2)
{
return param1+param2;
}
public String echo(String message)
{
return "hello: "+message;
}
public static void main(String[] args) throws Exception
{
Class<?> classType=InovkeTester.class;
Object invokeTester=classType.newInstance();
Method addMethod=classType.getMethod("add",new Class[]{int.class,int.class});
Object result=addMethod.invoke(invokeTester,new Object[]{1,2});
System.out.println( (Integer)result );
Method echoMethod=classType.getMethod("echo",new Class[]{String.class});
Object result2=echoMethod.invoke(invokeTester,new Object[]{"tom"});
System.out.println( (String)result2 );
}
}
java 反射
最新推荐文章于 2024-08-14 23:12:09 发布