`
奎河少年
  • 浏览: 23648 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java反射(一)

 
阅读更多
import java.lang.reflect.Method;

public class DumpMethod {
	public int add(int a, int b) {
		return a + b;
	}

	public String echo(String message) {
		return "Hello" + " " + message;
	}

	public static void main(String[] args) throws Exception {
		Class<?> classType = DumpMethod.class;
		Object dumpMethod = classType.newInstance();
		Method addMethod = classType.getMethod("add", new Class[] { int.class,
				int.class });
		Object result = addMethod.invoke(dumpMethod, new Object[] { 1, 2 });
		System.out.println((Integer) result);
		System.out.println("------------------------------");
		Method echoMethod = classType.getMethod("echo",
				new Class[] { String.class });
		Object result2 = echoMethod.invoke(dumpMethod, new Object[] { "Tom" });
		System.out.println((String) result2);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics