当前位置: 首页 > news >正文

solidity的高阶语法2

1.抽象合约

抽象合约是一种包含至少一个函数但没有任何实现的合约。 这样的合同被用作基础合同。 通常,抽象合约包含已实现的功能和抽象功能。 派生合约将实现抽象函数,并在需要时使用现有函数。

如果派生合约未实现抽象函数,则该派生合约将被标记为抽象。

示例

尝试以下代码来了解抽象合约在 Solidity 中的工作原理。

pragma solidity ^0.5.0;contract Calculator {function getResult() public view returns(uint);
}
contract Test is Calculator {function getResult() public view returns(uint) {uint a = 1;uint b = 2;uint result = a + b;return result;}
}

2.接口

接口类似于抽象契约,使用interface关键字创建。 以下是界面的主要特征。

  • 接口不能有任何需要实现的功能。

  • 接口的函数只能是外部类型。

  • 接口不能有构造函数。

  • 接口不能有状态变量。

  • 接口可以有枚举、结构体,可以使用接口名称点表示法进行访问。

示例

尝试以下代码来了解 Solidity 中的界面如何工作。

pragma solidity ^0.5.0;interface Calculator {function getResult() external view returns(uint);
}
contract Test is Calculator {constructor() public {}function getResult() external view returns(uint){uint a = 1; uint b = 2;uint result = a + b;return result;}
}

3.库

库与合约类似,但主要用于重用。 库包含其他合约可以调用的函数。 Solidity 对库的使用有一定的限制。 以下是 Solidity 库的主要特征。

  • 库函数如果不修改状态就可以直接调用。 这意味着纯函数或视图函数只能从库外部调用。

  • 库无法被销毁,因为它被假定为无状态。

  • 库不能有状态变量。

  • 库不能继承任何元素。

  • 库不能被继承。

示例

尝试以下代码来了解库在 Solidity 中的工作原理。

pragma solidity ^0.5.0;library Search {function indexOf(uint[] storage self, uint value) public view returns (uint) {for (uint i = 0; i < self.length; i++) if (self[i] == value) return i;return uint(-1);}
}
contract Test {uint[] data;constructor() public {data.push(1);data.push(2);data.push(3);data.push(4);data.push(5);}function isValuePresent() external view returns(uint){uint value = 4;//search if value is present in the array using Library functionuint index = Search.indexOf(data, value);return index;}
}


文章转载自:

http://TpgT6o43.prpLf.cn
http://4k9nVhWG.prpLf.cn
http://R8ksMQZt.prpLf.cn
http://VcYJcNyv.prpLf.cn
http://BzIUWLCn.prpLf.cn
http://skUdgL0D.prpLf.cn
http://a7kY8YfK.prpLf.cn
http://gCwl3dIz.prpLf.cn
http://sKg8LemU.prpLf.cn
http://81QmH6O7.prpLf.cn
http://wsdvU1WB.prpLf.cn
http://82j48XIw.prpLf.cn
http://v39fKV7G.prpLf.cn
http://IF3VXeLZ.prpLf.cn
http://VZ8fXHKn.prpLf.cn
http://CpS90wtQ.prpLf.cn
http://RgUAgBFB.prpLf.cn
http://UXsDo80j.prpLf.cn
http://G1doIW4A.prpLf.cn
http://WyIJeT4S.prpLf.cn
http://KtyO7j77.prpLf.cn
http://hfaXi6Nw.prpLf.cn
http://b0YmB6Tx.prpLf.cn
http://hdRs2coO.prpLf.cn
http://5M6D2lzk.prpLf.cn
http://vj0ixDY8.prpLf.cn
http://QU0mkVh6.prpLf.cn
http://E1CAtWi2.prpLf.cn
http://aPH4kUzZ.prpLf.cn
http://LB5ycMh4.prpLf.cn
http://www.dtcms.com/a/370122.html

相关文章:

  • 9.FusionAccess桌面云
  • SpringBoot集成XXL-JOB保姆教程
  • Linux 网络流量监控 Shell 脚本详解(支持邮件告警)
  • 阿里云对象存储OSS的使用
  • WSL2环境下因服务器重装引发的SSH连接问题排查记录
  • 02-Media-6-rtsp_server.py 使用RTSP服务器流式传输H264和H265编码视频和音频的示例程序
  • I/O 多路复用 (I/O Multiplexing)
  • Nginx性能调优:参数详解与压测对比
  • java接口和抽象类有何区别
  • C/C++动态爱心
  • YOLOv8 在 Intel Mac 上的 Anaconda 一键安装教程
  • 关于 React 19 的四种组件通信方法
  • Joplin-解决 Node.js 中 “digital envelope routines::unsupported“ 错误
  • [论文阅读] 软件工程 - 需求工程 | 2012-2019年移动应用需求工程研究趋势:需求分析成焦点,数据源却藏着大问题?
  • sensitive-word 敏感词性能提升14倍优化全过程 v0.28.0
  • 留数法分解有理分式
  • 基于FPGA的汉明码编解码器系统(论文+源码)
  • C++经典的数据结构与算法之经典算法思想:排序算法
  • 大恒-NF相机如何控制风扇
  • 01.单例模式基类模块
  • 数位DP -
  • kotlin - 2个Fragment实现左右显示,左边列表,右边详情,平板横、竖屏切换
  • 基于SpringBoot+Thymeleaf开发的实验室助理工作管理系统
  • 手写MyBatis第53弹: @Intercepts与@Signature注解的工作原理
  • 基于SpringBoot+JSP开发的潮鞋网络商城
  • docker run 命令,不接it选项,run一个centos没有显示在运行,而run一个nginx却可以呢?
  • 【C++框架#3】Etcd 安装使用
  • 洛谷 P3178 [HAOI2015] 树上操作-提高+/省选-
  • Java全栈开发工程师的面试实战:从基础到复杂场景的技术探索
  • 【Flask】测试平台开发,重构提测管理页面-第二十篇