隨筆 - 40, 文章 - 0, 評論 - 20, 引用 - 0
          數據加載中……

          執行過程中動態執行方法

          在編程中可能回碰到一些到實際運行時才指定要調用的方法的需要,最典型的是Struct的DispatchAction類,
          它能根據用戶頁面請求參數的不同來調用不同的方法來處理用戶請求。我下面寫了一個簡單的例子來簡單演示其
          實現方法:

          package learn;
          import java.lang.NoSuchMethodException;
          import java.lang.reflect.Method;
          import java.util.Date;

          public class TestMethod{
              protected Class clazz = this.getClass();
              protected Class[] no_parameter = {};  //方法無參數
              protected Class[] string_parameter = {String.class}; //以String 為參數
              protected Class[] int_parameter = {int.class};  //以int為參數
              protected Class[] multi_parameter = {String.class,Date.class}; //多個參數,第一個為String,第二二個為Date   
             
              public void method1(){
                System.out.println("method1");
              }
             
              public void method2(String str){
                System.out.println("method2=>"+str);
              }
             
              public void method3(int i){
                System.out.println("method2=>"+i);
              }
             
              public void method4(String str,Date date){
                System.out.println("method4=>"+str+"==="+date.toLocaleString());
              }
             
              public void execute(String methodName,int type,java.util.List list) throws Exception{
                try {
                  Method  m = getMethod(methodName,type);
                  int size = (list != null )? list.size():0;
                  Object o [] = new Object[size];
                  for( int i =0 ; i< size ; i++ )
                    o[i] = list.get(i);  //準備參數
                  m.invoke(this,o);
                }
                catch (Exception ex) {
                  ex.printStackTrace();
                  throw new Exception(ex.getMessage());
                }
              }
             
              private Method getMethod(String name,int type)throws NoSuchMethodException{
                  Method m = null;
                  switch(type){
                    case 1:
                      m = clazz.getMethod(name,no_parameter);
                      break;
                    case 2:
                      m = clazz.getMethod(name,string_parameter);
                      break;
                    case 3:
                      m = clazz.getMethod(name,int_parameter);
                      break;
                    case 4:
                      m = clazz.getMethod(name,multi_parameter);
                      break;
                    default:
                      m = clazz.getMethod(name,no_parameter);
                  }
                  return m;
              }
             
              public static void main(String [] argv){
                TestMethod testMethod = new TestMethod();
                try{
                  java.util.List list = new java.util.ArrayList();
                  testMethod.execute("method1", 1, list);
                  list.add("
          http://www.aygfsteel.com/minmoon");
                  testMethod.execute("method2", 2, list);
                  list.clear();
                  list.add("
          mailTo:xiaoliang@aspire-tech.com");
                  list.add(new Date());
                  testMethod.execute("method4",4,list);
                }
                catch(Exception e){
                  e.printStackTrace();
                }
              }
          }

          其中幾個關鍵的地方說明一下:
            clazz.getMethod(name,multi_parameter);  其中multi_parameter是要根據實際方法的參數來定義的。
            m.invoke(this,o);   o是要傳入方法的參數列表。

          posted on 2005-11-23 00:18 月亮 閱讀(487) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 梁河县| 黄冈市| 奉节县| 开远市| 遂昌县| 启东市| 沈丘县| 延庆县| 南漳县| 越西县| 兴隆县| 赣州市| 澄迈县| 香格里拉县| 桦南县| 平潭县| 方城县| 怀集县| 扶绥县| 乐业县| 灵山县| 诸暨市| 太谷县| 同仁县| 温宿县| 汨罗市| 东辽县| 金塔县| 阿勒泰市| 宁阳县| 抚宁县| 三明市| 托里县| 饶阳县| 平邑县| 兴山县| 蒙山县| 青铜峡市| 阜城县| 南江县| 棋牌|