<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### Move Field(搬移值域) (譯注:本節大量保留class,method,source,target等字眼) 你的程序中,某個field(值域〕被其所駐class之外的另一個class更多地用到。 在target class 建立一個new field,修改source field的所有用戶,令它們改用此new field。 ![](https://box.kancloud.cn/2016-08-15_57b1b56c20a0f.gif) **動機(Motivation)** 在classes之間移動狀態(states)和行為,是重構過程中必不可少的措施。隨著系統發展,你會發現自己需要新的class,并需要將原本的工作責任拖到新的class中。這個星期中合理而正確的設計決策,到了下個星期可能不再正確。這沒問題;如果你從來沒遇到這種情況,那才有問題。 如果我發現,對于一個field(值域),在其所駐class之外的另一個class中有更多函數使用了它,我就會考慮搬移這個field。上述所謂「使用」可能是通過設值/取值(setting/getting)函數間接進行。我也可能移動該field的用戶(某函數),這取決于是否需要保持接口不受變化。如果這些函數看上去很適合待在原地,我就選擇搬移field。 使用Extract Class 時,我也可能需要搬移field。此時我會先搬移field,然后再搬移函數。 **作法(Mechanics)** - 如果field的屬性是public,首先使用Encapsulate Field 將它封裝起來。 - 如果你有可能移動那些頻繁訪問該field的函數,或如果有許多函數訪問某個field,先使用Self Encapsulate Field 也許會有幫助。 - 編譯,測試。 - 在target class中建立與source field相同的field,并同時建立相應的設值/取值 (setting/getting)函數。 - 編譯target class。 - 決定如何在source object中引用target object。 - 一個現成的field或method可以助你得到target object。如果沒有,就看能否輕易建立這樣一個函數。如果還不行,就得在source class中新建一個field來存放target object。這可能是個永久性修改,但你也可以暫不公開它,因為后續重構可能會把這個新建field除掉。 - 刪除source field。 - 將所有「對source field的引用」替換為「對target適當函數的調用」。 - 如果是「讀取」該變量,就把「對source field的引用」替換為「對target取值函數(getter)的調用」;如果是「賦值」該變量,就把對source field的引用」替換成「對設值函數(setter)的調用」。 - 如果source field不是private,就必須在source class的所有subclasses中查找source field的引用點,并進行相應替換。 - 編譯,測試。 **范例(Examples)** 下面是Account class的部分代碼: ~~~ class Account... private AccountType _type; private double _interestRate; double interestForAmount_days (double amount, int days) { return _interestRate * amount * days / 365; } ~~~ 我想把表示利率的_interestRate搬移到AccountType class去。目前已有數個函數引用了它,interestForAmount_days() 就是其一。下一步我要在AccountType中建立_interestRate field以及相應的訪問函數: ~~~ class AccountType... private double _interestRate; void setInterestRate (double arg) { _interestRate = arg; } double getInterestRate () { return _interestRate; } ~~~ 這時候我可以編譯新的AccountType class。 現在,我需要讓Account class中訪問此_interestRate field的函數轉而使用AccountType對象,然后刪除Account class中的_interestRate field。我必須刪除source field,才能保證其訪問函數的確改變了操作對象,因為編譯器會幫我指出未正確獲得修改的函數。 ~~~ private double _interestRate; double interestForAmount_days (double amount, int days) { return _type.getInterestRate() * amount * days / 365; } ~~~ **范例:使用Self Encapsulate(自我封裝)** 如果有很多函數已經使用了_interestRate field,我應該先運用Self Encapsulate Field: ~~~ class Account... private AccountType _type; private double _interestRate; double interestForAmount_days (double amount, int days) { return getInterestRate() * amount * days / 365; } private void setInterestRate (double arg) { _interestRate = arg; } private double getInterestRate () { return _interestRate; } ~~~ 這樣,在搬移field之后,我就只需要修改訪問函數(accessors)就行了 : ~~~ double interestForAmountAndDays (double amount, int days) { return getInterestRate() * amount * days / 365; } private void setInterestRate (double arg) { _type.setInterestRate(arg); } private double getInterestRate () { return _type.getInterestRate(); } ~~~ 如果以后有必要,我可以修改訪問函數(accessors)的用戶,讓它們使用新對象。 Self Encapsulate Field 使我得以保持小步前進。如果我需要對做許多處理,保持小步前進是有幫助的。特別值得一提的是:首先使用Self Encapsulate Field 使我得以更輕松使用Move Method 將函數搬移到target class中。如果待移函數引用了field的訪問函數(accessors),那么那些引用點是無須修 改的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看