ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          范例(Examples)
          我從下列這段簡單代碼開始:
          int discount(int inputVal, int quantity, int yearToDate) {
              if(inputVal > 50) inputVal -= 2;
              if(quantity > 100) inputVal -= 1;
              if(yearToDate > 1000) inputVal -= 4;
              return inputVal;
          }
          以臨時變量取代對參數(shù)的賦值動作,得到下列代碼:
           int discount(int inputVal, int quantity, int yearToDate) {
              int result = inputVal;
              if(inputVal > 50)
          result -= 2;
              if(quantity > 100) result -= 1;
              if(yearToDate > 1000) result -= 4;
              return result
          }
          還可以為參數(shù)加上關(guān)鍵詞final,從而強制它遵循[不對參數(shù)賦值]這一慣例:
            int discount(final int inputVal, final int quantity, final int yearToDate) {
              int result = inputVal;
              if(inputVal > 50)
          result -= 2;
              if(quantity > 100) result -= 1;
              if(yearToDate > 1000) result -= 4;
              return result
          }

          不過我的承認(rèn),我并不經(jīng)常使用final來修飾參數(shù),因為我發(fā)現(xiàn),對于提高短函數(shù)的清晰度,這個辦法并無太大幫助。我通常會在較長的函數(shù)中使用它,讓它幫助我檢查參數(shù)是否被做了修改。
          posted on 2005-08-29 15:40 ivaneeo 閱讀(208) 評論(0)  編輯  收藏 所屬分類: refactoring-從地獄中重生
          主站蜘蛛池模板: 明星| 阳谷县| 谢通门县| 德阳市| 东兴市| 潍坊市| 五台县| 玛沁县| 江源县| 泌阳县| 会宁县| 东宁县| 汤阴县| 宜君县| 怀仁县| 电白县| 营山县| 兴义市| 从化市| 富民县| 阳山县| 宁海县| 阳原县| 焉耆| 新乐市| 元江| 贞丰县| 涡阳县| 通榆县| 兴义市| 峡江县| 黄骅市| 泾阳县| 临邑县| 八宿县| 乌拉特中旗| 武强县| 茌平县| 利川市| 神农架林区| 阳高县|