隨筆-55  評論-208  文章-0  trackbacks-0
          問題是這樣的,我看到原來有一個方法是根據(jù)name的名稱調(diào)用getName()這個方法。
          然后方法的實現(xiàn)是使用字符串的判斷來實現(xiàn)的
          代碼如下:
          public Object getOrderby() {
                  
          if (ORDER_TITLE.equals(this.ordered)) {
                      
          return getTitle();
                  }
           else if (ORDER_RENDERDATE.equals(this.ordered)) {
                      
          return getRenderDate();
                  }
           else if (ORDER_CREATEDATE.equals(this.ordered)) {
                      
          return getCreateDate();
                  }
           else if (ORDER_LASTMODIFIED.equals(this.ordered)) {
                      
          return getLastModified();
                  }
           else {
                      
          return getTitle();
                  }

              }

          這樣的話如果以后新增get方法就意味著要增加這個getOrderby()方法的判斷。
          所以我決定用類反射來改造這個方法,首先定義一個doGetMethod的方法
           private Object doGetMethod(String methodName) throws Exception {
                  Object result 
          = null;
                  Object[] eo 
          = new Object[] {};
                  String getMethodName 
          = "get" + methodName.toLowerCase();
                  Method[] methods 
          = getClass().getMethods();
                  
          for (int i = 0; i < methods.length; i++{
                      Method method 
          = methods[i];
                      
          if (getMethodName.equals(method.getName().toLowerCase())) {
                          result 
          = method.invoke(this, eo);
                      }

                  }

                  
          return result;
              }
          這里根據(jù)methodName組合成getMethodName,然后到Method[]中找這個Method,然后用invoke調(diào)用這個方法
          這里要說明invoke這個方法的參數(shù)是第一個參數(shù)是指定包含方法的類,第二個參數(shù)是指定方法需要的參數(shù)。
          最后修改原來的getOrderBy
            public Object getOrderby() {
                  Object obj 
          = getTitle();
                  
          try {
                      obj 
          = doGetMethod(getOrdered());
                  }
           catch (Exception e) {
                      
          return obj;
                  }

                  
          return obj;
              }
          posted on 2007-04-12 15:36 rocket 閱讀(1919) 評論(3)  編輯  收藏

          評論:
          # re: 根據(jù)name調(diào)用getName方法 2007-04-12 17:36 | G_G
          沙發(fā)
            回復  更多評論
            
          # re: 根據(jù)name調(diào)用getName方法 2007-04-12 17:38 | popoer
          good!  回復  更多評論
            
          # re: 根據(jù)name調(diào)用getName方法 2007-04-12 19:57 | 劉甘泉
          把簡單的東西弄復雜了,如果方法固定的話 用 if else 效果更好,如果需要隨時更改的話,用interface 不同實現(xiàn) +反射都好用的多  回復  更多評論
            

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 桐乡市| 曲周县| 塔河县| 泽州县| 洪泽县| 饶河县| 钦州市| 鱼台县| 德清县| 庆安县| 吴堡县| 信阳市| 景宁| 泸溪县| 长治县| 定结县| 珲春市| 两当县| 临猗县| 拉孜县| 左权县| 来凤县| 泸西县| 广灵县| 宁都县| 庆云县| 景洪市| 志丹县| 房产| 大名县| 牙克石市| 诸城市| 阜南县| 临沂市| 扬州市| 故城县| 巴中市| 开平市| 新巴尔虎右旗| 惠安县| 平定县|