您好,登錄后才能下訂單哦!
modifire還可以帶參數(shù),如下面的例子,模擬了游戲中的升級(jí)操作。如果玩家等級(jí)達(dá)到2級(jí),就可以修改名字。如果玩家等級(jí)達(dá)到10級(jí),就可以修改DNA。
通過(guò)帶參數(shù)的modifire實(shí)現(xiàn)對(duì)于代碼的封裝。
1 | pragma solidity^0.4.23; |
通過(guò)上面的例子,我們能夠看到帶參數(shù)的modifire的使用方法。首先在modifire中添加參數(shù)。接著在函數(shù)定義中,在修飾符與return返回值之間加上controlLevel(傳遞的參數(shù))
.
例如,當(dāng)調(diào)用changeName函數(shù)執(zhí)行的語(yǔ)句為:
1 | require(level>2); |
例如,當(dāng)調(diào)用changeDNA函數(shù)執(zhí)行的語(yǔ)句為:
1 | require(level>10); |
函數(shù)可以有多個(gè)modifire,這種情況要相對(duì)復(fù)雜得多。
我們從下面的例子,來(lái)講解多重modifire的執(zhí)行過(guò)程。
1 | contract mulmodifiererDeep{ |
下面的例子中,當(dāng)執(zhí)行test方法后,狀態(tài)變量a的值會(huì)變?yōu)?. 我們提到過(guò),modifire中的下劃線(xiàn)指代了函數(shù)中的所有語(yǔ)句。
所以執(zhí)行流程為a = 1 a = 100 a = 2
下面的例子中,使用了多重的modire。
1 | contract mulmodifiererDeep2{ |
在上面的例子中test函數(shù)使用了modifire:mod1、mod2
嵌套規(guī)則為,首先函數(shù)中的a = 100嵌套到mod2的_中。整個(gè)語(yǔ)句變?yōu)榱?br/>a = 3; a = 100; a = 4;
接下來(lái),將上面的語(yǔ)句添加到mod1的下劃線(xiàn)中,所以整個(gè)執(zhí)行語(yǔ)句變?yōu)榱?/p>
1 | a = 1; |
最后執(zhí)行結(jié)果為 a = 2。
多重modifire的順序非常的重要的。
下面的例子,和案例2相同但是修改了mod1與mod2在函數(shù)中的順序。
1 | contract mulmodifiererDeep2{ |
在上面的例子中test函數(shù)使用了modifire:mod2、mod1
嵌套規(guī)則為,首先函數(shù)中的a = 100嵌套到mod1的_中。整個(gè)語(yǔ)句變?yōu)榱?br/>a = 1; a = 100; a = 2;
接下來(lái),將上面的語(yǔ)句添加到mod1的下劃線(xiàn)中,所以整個(gè)執(zhí)行語(yǔ)句變?yōu)榱?/p>
1 | a = 3; |
最后執(zhí)行結(jié)果為 a = 4。
本文鏈接: https://dreamerjonson.com/2018/11/22/solidity-33-modifire-deep/
版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉(zhuǎn)載請(qǐng)注明出處!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。