溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

solidity智能合約[36]-連續繼承與多重繼承

發布時間:2020-07-23 08:03:09 來源:網絡 閱讀:772 作者:jonson_jackson 欄目:開發技術

連續繼承

合約可以被連續的繼承,在下面的合約中,father繼承了grandfather、son繼承了father。那么son也同樣繼承了grandfather中的狀態變量和方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
contract grandfather{
   uint public  money=10000;
   function dahan() public pure returns(string){
       return "dahan";
   }
}

contract father is grandfather{

}
contract son is father{

}

連續繼承重名問題

下面的合約中,grandfather合約與 father合約中狀態變量的名字、函數的名字都是相同的,這時,son中的狀態變量money和繼承的函數 以父類father合約中的狀態變量和函數為準。

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 grandfather{
   uint public  money=10000;
   function dahan() public pure returns(string){
       return "dahan";
   }
}

contract father is grandfather{
     uint public  money=9999;
    function dahan() public pure returns(string){
       return "alice";
   }
}

contract son is father{
 function getMonry() returns(uint){
       return money;
   }
}

多重繼承

合約可以繼承多個合約,也可以被多個合約繼承。如下所示:

1
2
3
4
5
6
7
8
9
10
contract father{
}


contract mother{
}

contract son is father,mother{

}

多重繼承有重名

多重繼承有重名時,繼承的順序時很重要的,以最后繼承的為主。例如下面的例子中,son合約最后繼承了mother,因此以mother合約中的money=8888為準。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
contract father is grandfather{
     uint public  money=9999;

    function dahan() public pure returns(string){
       return "alice";
   }
}


contract mother{
   uint public money=8888;
   uint public weight=100;

}

contract son is father,mother{

}
  • 本文鏈接: https://dreamerjonson.com/2018/11/22/solidity-36-inheritdeep/

  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協議 許可協議。轉載請注明出處!

solidity智能合約[36]-連續繼承與多重繼承

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女