solidity智能合约[36]-连续继承与多重继承

发布时间:2020-07-23 08:03:09 作者:jonson_jackson
来源:网络 阅读:713

连续继承

合约可以被连续的继承,在下面的合约中,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{

}

solidity智能合约[36]-连续继承与多重继承

推荐阅读:
  1. 使用Java SDK实现离线签名
  2. 如何在以太坊上发行自己的代币

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

区块链 solidity 智能合约

上一篇:针对Oracle数据库版本12.1.0.1,11.2.0.3及更早版本的推荐修补程序和操作

下一篇:使用js抓取今日头条的文章

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》