千家信息网

olidity语言开发中的继承怎么使用

发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇文章主要讲解了"olidity语言开发中的继承怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"olidity语言开发中的继承怎么使用"吧!在
千家信息网最后更新 2025年02月23日olidity语言开发中的继承怎么使用

这篇文章主要讲解了"olidity语言开发中的继承怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"olidity语言开发中的继承怎么使用"吧!

在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,默认情况下是函数,或者可以从派生类访问。与其他编程语言一样,你可以指定从外部或派生合约中可以访问的内容。函数可以指定为externalpublicinternalprivate,默认为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智能合约了。

感谢各位的阅读,以上就是"olidity语言开发中的继承怎么使用"的内容了,经过本文的学习后,相信大家对olidity语言开发中的继承怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

合约 智能 函数 语言 代码 开发 内容 变量 情况 状态 顺序 学习 重要 两个 作用 名称 基础 意味 接口 编程语言 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 快猫社区关服务器了 聊天app的服务器连接在哪 南京理工大学网络技术与应用 芯片服务器首次入围运营采集商 1.介绍市面上主流的云服务器并比较 指挥中心网络安全大屏 亳州保险软件开发哪家好 校园网络安全反馈 鸠鸠互联网科技有限公司范总 铁路计算机软件开发是干什么的 tomcat服务器的功能 数据库触发器修改插入的值 网络安全进入校园美术画 未来之战哪个服务器最好 怎么对比两组表格的数据库 软件开发编程常用英语 青浦区参考网络技术创新服务 千阳网络技术有限公司邮编 深圳深圳关于软件开发 服务器磁带备份教程 谣言识别软件开发 大姆指互联网产业科技集团 数据库报1060错误 网络安全法规定最新 网络安全刑事犯罪案例 徐州手机软件开发项目 如皋市志雷网络技术工作室 腾讯云服务器香港 小班护苗网络安全课教案 江苏第三方软件开发服务
0