Java Reflect

时间:2014-10-06 21:46:00   收藏:0   阅读:242
 1 package observer;
 2 
 3 import java.lang.reflect.Method;
 4 
 5 import oracle.net.aso.c;
 6 
 7 public class Test {
 8 
 9     public static void main(String[] args) throws Exception {
10         
11         String str = "observer.Dad";
12         Class cls = Class.forName(str);
13         Object obj = cls.newInstance();
14         Method[] methods = cls.getMethods();
15         for(Method m:methods)
16         {
17             if(m.getName().equals("ActionToWakeup"))
18             {
19                 m.invoke(obj, new WakeupEvent(0, "", null));
20             }
21         }
22     }
23 }
1 package observer;
2 
3 public class Dad{
4     @Override
5     public void ActionToWakeup(WakeupEvent e) {
6         System.out.println("儿子醒了,我要去喂奶了。。。");
7     }
8 }

bubuko.com,布布扣

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!