<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 函數修改器(Function Modifiers) `修改器(Modifiers)`可以用來輕易的改變一個函數的行為。比如用于在函數執行前檢查某種前置條件。修改器是一種合約屬性,可被繼承,同時還可被派生的合約`重寫(override)`。下面我們來看一段示例代碼: ``` pragma solidity ^0.4.0; contract owned { function owned() { owner = msg.sender; } address owner; // This contract only defines a modifier but does not use // it - it will be used in derived contracts. // The function body is inserted where the special symbol // "_;" in the definition of a modifier appears. // This means that if the owner calls this function, the // function is executed and otherwise, an exception is // thrown. modifier onlyOwner { if (msg.sender != owner) throw; _; } } contract mortal is owned { // This contract inherits the "onlyOwner"-modifier from // "owned" and applies it to the "close"-function, which // causes that calls to "close" only have an effect if // they are made by the stored owner. function close() onlyOwner { selfdestruct(owner); } } contract priced { // Modifiers can receive arguments: modifier costs(uint price) { if (msg.value &gt;= price) { _; } } } contract Register is priced, owned { mapping (address =&gt; bool) registeredAddresses; uint price; function Register(uint initialPrice) { price = initialPrice; } // It is important to also provide the // "payable" keyword here, otherwise the function will // automatically reject all Ether sent to it. function register() payable costs(price) { registeredAddresses[msg.sender] = true; } function changePrice(uint _price) onlyOwner { price = _price; } } ``` 修改器可以被繼承,使用將`modifier`置于參數后,返回值前即可。 特殊`_`表示使用修改符的函數體的替換位置。 從合約`Register`可以看出全約可以多繼承,通過`,`號分隔兩個被繼承的對象。 修改器也是可以接收參數的,如`priced`的`costs`。 使用修改器實現的一個防重復進入的例子。 ``` pragma solidity ^0.4.0; contract Mutex { bool locked; modifier noReentrancy() { if (locked) throw; locked = true; _; locked = false; } /// This function is protected by a mutex, which means that /// reentrant calls from within msg.sender.call cannot call f again. /// The `return 7` statement assigns 7 to the return value but still /// executes the statement `locked = false` in the modifier. function f() noReentrancy returns (uint) { if (!msg.sender.call()) throw; return 7; } } ``` 例子中,由于`call()`方法有可能會調回當前方法,修改器實現了防重入的檢查。 如果同一個函數有多個修改器,他們之間以空格隔開,修飾器會依次檢查執行。 > 需要注意的是,在Solidity的早期版本中,有修改器的函數,它的`return`語句的行為有些不同。 在修改器中和函數體內的顯式的`return`語句,僅僅跳出當前的修改器和函數體。返回的變量會被賦值,但整個執行邏輯會在前一個修改器后面定義的"_"后繼續執行。 修改器的參數可以是任意表達式。在對應的上下文中,所有的函數中引入的符號,在修改器中均可見。但修改器中引入的符號在函數中不可見,因為它們有可能被重寫。
                  <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>

                              哎呀哎呀视频在线观看