首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Reflection 照射

2012-12-20 
Reflection 映射public class TestReflect {???? public int sum(int m,int n){???????? return m+n????

Reflection 映射

public class TestReflect {
???? public int sum(int m,int n){
???????? return m+n;
???? }
?
???? public String say(String name){
??? ???? return name+": haha!";
??? ?}
?
??? ?public static void main(String[] args) throws Exception {
??????? ?Class cla = TestReflect.class;
????? ???Method sumMethod = cla.getMethod("sum", new Class[]{int.class,int.class});
????? ???int sum = (Integer) sumMethod.invoke(cla.newInstance(), new Object[]{1,2});
??????? ?Method sayMethod = cla.getMethod("say", new Class[]{String.class});
??????? ?String word = (String) sayMethod.invoke(cla.newInstance(), new Object[]{"I"});
???????? System.out.println(sum);
??????? ?System.out.println(word);
?? ?}
}

?

热点排行