合约可以被连续的继承,在下面的合约中,father继承了grandfather、son继承了father。那么son也同样继承了grandfather中的状态变量和方法。
12345678910111213 | 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合约中的状态变量和函数为准。
12345678910111213141516171819202122 | 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; }} |
合约可以继承多个合约,也可以被多个合约继承。如下所示:
12345678910 | contract father{}contract mother{}contract son is father,mother{} |
多重继承有重名时,继承的顺序时很重要的,以最后继承的为主。例如下面的例子中,son合约最后继承了mother,因此以mother合约中的money=8888为准。
123456789101112131415161718 | 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协议 许可协议。转载请注明出处!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。