solidity语言开发以太坊智能合约中的继承
我们已经探索了很多主题,在编写智能合约时我们发现经常使用相同的模式:例如,智能合约具有在构造函数中设置的所有者,然后生成修改器以便仅让所有者使用一些功能。如果我们制定实施这些功能的基础合约并在未来的智能合约中重复使用它们那该怎么办?你一定猜得到,我们将使用继承。
在Solidity中,继承与经典的面向对象编程语言非常相似。你首先编写基本智能合约并告知你的新智能合约将从基础合约继承。
你还必须通过复制包含多态的代码来了解Solidity支持多重继承。所有函数调用都是虚函数,这意味着会是调用派生函数最多的函数,除非明确给出了合约名称。当某一个智能合约从多个合约继承时,只在区块链上创建一个智能合约,并将所有基础合约中的代码复制到创建的智能合约中。
让我们写下我们的基本智能合约:它将让我们轻松地为我们的合约添加所有权。我们将其命名为Ownable
。OpenZeppelin的员工写了很多可以在智能合约中使用的可重用代码。这些代码段可通过其工具或其Github存储库获得。
这是代码:
pragma solidity ^0.4.11; /** * @title Ownable * @dev The Ownable contract has an owner address, and provides basic authorization control * functions, this simplifies the implementation of "user permissions". */ contract Ownable { address public owner; /** * @dev The Ownable constructor sets the original `owner` of the contract to the sender * account. */ function Ownable() { owner = msg.sender; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { require(msg.sender == owner); _; } /** * @dev Allows the current owner to transfer control of the contract to a newOwner. * @param newOwner The address to transfer ownership to. */ function transferOwnership(address newOwner) onlyOwner { require(newOwner != address(0)); owner = newOwner; } }
我们经常写的另一种模式是破坏我们的合约并将合约中存储的资金转移给所有者或另一个地址的能力。重要的是我们不希望任何人能够破坏我们的合约,所以我们的Destructible
应该继承Ownable
。继承是使用智能合约名称后面的is
关键字完成的。
必须注意,它是Solidity,默认情况下是函数,或者可以从派生类访问。与其他编程语言一样,你可以指定从外部或派生合约中可以访问的内容。函数可以指定为external
,public
,internal
,private
,默认为public
。
external
:外部函数是智能合约接口的一部分,这意味着可以从其他合约和交易中调用它们。external
函数f不能在内部调用(即f()不起作用,但this.f()起作用)。当外部函数接收大量数据时,它们有时会更有效。public
:公共函数是智能合约接口的一部分,可以在内部调用,也可以通过消息调用。对于公共状态变量,会生成自动getter函数(见下文)。internal
:这些函数和状态变量只能在内部访问(即从当前合约或从中派生的合约中),而其他情况不使用它。private
:私有函数和状态变量仅对定义它们的智能合约可见,而不是在派生合约中可见。
下面是我们的第二份智能合约:
pragma solidity ^0.4.11; /** * @title Destructible * @dev Base contract that can be destroyed by owner. All funds in contract will be sent to the owner. */ contract Destructible is Ownable { function Destructible() payable { } /** * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() onlyOwner { selfdestruct(owner); } function destroyAndSend(address _recipient) onlyOwner { selfdestruct(_recipient); } }
现在使用这两个基本合约,我们将写一个简单的BankAccount
智能合约,人们可以汇款,业主可以提取。
pragma solidity ^0.4.11; contract BankAccount is Ownable, Destructible { function store() public payable { } function withdraw(uint amount) public onlyOwner { if (this.balance >= amount) { msg.sender.transfer(amount); } } }
请注意,我们需要从两个智能合约继承。继承的顺序很重要。判断顺序的一个简单规则是按照“最类似基类”到“最多派生”的顺序指定基类。
以下是我们将部署的整个代码:
pragma solidity ^0.4.11; /** * @title Ownable * @dev The Ownable contract has an owner address, and provides basic authorization control * functions, this simplifies the implementation of "user permissions". */ contract Ownable { address public owner; /** * @dev The Ownable constructor sets the original `owner` of the contract to the sender * account. */ function Ownable() { owner = msg.sender; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { require(msg.sender == owner); _; } /** * @dev Allows the current owner to transfer control of the contract to a newOwner. * @param newOwner The address to transfer ownership to. */ function transferOwnership(address newOwner) onlyOwner { require(newOwner != address(0)); owner = newOwner; } } /** * @title Destructible * @dev Base contract that can be destroyed by owner. All funds in contract will be sent to the owner. */ contract Destructible is Ownable { function Destructible() payable { } /** * @dev Transfers the current balance to the owner and terminates the contract. */ function destroy() onlyOwner { selfdestruct(owner); } function destroyAndSend(address _recipient) onlyOwner { selfdestruct(_recipient); } } contract BankAccount is Ownable, Destructible { function store() public payable { } function withdraw(uint amount) public onlyOwner { if (this.balance >= amount) { msg.sender.transfer(amount); } } }
我们现在可以部署我们的银行账户bank account
智能合约了。
部署后,我们可以看到我们看到了我们的银行帐户功能,但也看到了继承的功能。
分享一些以太坊相关的交互式在线编程实战教程:
- java以太坊开发教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
- python以太坊,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。
- php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和交易等内容。
- 以太坊入门教程,主要介绍智能合约与dapp应用开发,适合入门。
- 以太坊开发进阶教程,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
- C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和交易等。
这里是原文solidity语言开发中的继承
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
一次 HashSet 所引起的并发问题
背景 上午刚到公司,准备开始一天的摸鱼之旅时突然收到了一封监控中心的邮件。 心中暗道不好,因为监控系统从来不会告诉我应用完美无 bug,其实系统挺猥琐。 打开邮件一看,果然告知我有一个应用的线程池队列达到阈值触发了报警。 由于这个应用出问题非常影响用户体验;于是立马让运维保留现场 dump 线程和内存同时重启应用,还好重启之后恢复正常。于是开始着手排查问题。 分析 首先了解下这个应用大概是做什么的。 简单来说就是从 MQ 中取出数据然后丢到后面的业务线程池中做具体的业务处理。 而报警的队列正好就是这个线程池的队列。 跟踪代码发现构建线程池的方式如下: ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, maxSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());; put(poolName,executor); 采用的是默认的 LinkedBlockingQueue 并没有指定大小(这也是个坑),于是这个队列的默认大...
- 下一篇
Android EventBus
本文节选自电子书《Netkiller Android 手札》 Netkiller Android 手札 http://www.netkiller.cn/android/index.html Mr.NeoChan,陈景峯(BG7NYT) 中国广东省深圳市望海路半岛城邦三期 518067 +8613113668890 <netkiller@msn.com> $Id: book.xml 606 2013-05-29 09:52:58Z netkiller $ 版权 © 2018 Neo Chan 版权声明 转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。 http://www.netkiller.cn http://netkiller.github.io http://netkiller.sourceforge.net 微信订阅号 netkiller-ebook (微信扫描二维码) QQ:13721218 请注明“读者” QQ群:128659835 请注明“读者” 2018-10 我的系列文档 编程语言 Netkiller Architect 手札 Net...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程