Java Reflect

 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 }

 

posted @ 2014-10-06 18:51  长孙无垢  阅读(196)  评论(0编辑  收藏  举报