溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

solidity智能合約[33]-modifire-deep

發(fā)布時(shí)間:2020-07-04 07:33:17 來(lái)源:網(wǎng)絡(luò) 閱讀:220 作者:jonson_jackson 欄目:開(kāi)發(fā)技術(shù)

帶參modifire

modifire還可以帶參數(shù),如下面的例子,模擬了游戲中的升級(jí)操作。如果玩家等級(jí)達(dá)到2級(jí),就可以修改名字。如果玩家等級(jí)達(dá)到10級(jí),就可以修改DNA。
通過(guò)帶參數(shù)的modifire實(shí)現(xiàn)對(duì)于代碼的封裝。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pragma solidity^0.4.23;

contract  modifiererParam{
   uint public level = 9;
   string public name;
   uint public DNA;

   modifier controlLevel(uint _needlevel){
       require(level>_needlevel);
       _;
   }

   //修改名字
   function changeName(string _name) public controlLevel(2){
       name = _name;
   }

   //修改DNA
     function changeDNA(uint _dna) public controlLevel(10){
       DNA = _dna;
   }
}

通過(guò)上面的例子,我們能夠看到帶參數(shù)的modifire的使用方法。首先在modifire中添加參數(shù)。接著在函數(shù)定義中,在修飾符與return返回值之間加上controlLevel(傳遞的參數(shù)).

例如,當(dāng)調(diào)用changeName函數(shù)執(zhí)行的語(yǔ)句為:

1
2
require(level>2);
name = _name;

例如,當(dāng)調(diào)用changeDNA函數(shù)執(zhí)行的語(yǔ)句為:

1
2
require(level>10);
name = _name;

多重modifire

函數(shù)可以有多個(gè)modifire,這種情況要相對(duì)復(fù)雜得多。
我們從下面的例子,來(lái)講解多重modifire的執(zhí)行過(guò)程。

案例1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
contract  mulmodifiererDeep{
   uint public a = 0;

   modifier mod1{
       a = 1;
       _;
       a = 2;
   }

   function test() public  mod1{

       a = 100;
   }

}

下面的例子中,當(dāng)執(zhí)行test方法后,狀態(tài)變量a的值會(huì)變?yōu)?. 我們提到過(guò),modifire中的下劃線(xiàn)指代了函數(shù)中的所有語(yǔ)句。
所以執(zhí)行流程為
a = 1 a = 100 a = 2

案例2

下面的例子中,使用了多重的modire。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
contract  mulmodifiererDeep2{
   uint public a = 0;

   modifier mod1{
       a = 1;
       _;
       a = 2;
   }

    modifier mod2{
       a = 3;
       _;
       a = 4;
   }

   function test() public  mod1 mod2{
       a = 100;
   }
}

在上面的例子中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
2
3
4
5
a = 1;
a = 3;
a = 100;
a = 4;
a = 2;

最后執(zhí)行結(jié)果為 a = 2。

案例3

多重modifire的順序非常的重要的。
下面的例子,和案例2相同但是修改了mod1與mod2在函數(shù)中的順序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
contract  mulmodifiererDeep2{
   uint public a = 0;

   modifier mod1{
       a = 1;
       _;
       a = 2;
   }

    modifier mod2{
       a = 3;
       _;
       a = 4;
   }

   function test() public  mod2 mod1{
       a = 100;
   }
}

在上面的例子中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
2
3
4
5
a = 3;
a = 1;
a = 100;
a = 2;
a = 4;

最后執(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)注明出處!

solidity智能合約[33]-modifire-deep

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI