当前位置:网站首页>solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
2022-07-01 14:46:00 【new life1937】
使用结构体和数组
创建新的结构体
struct Person {
uint age;
string name;
}
Person[] public people;
这个是Person 结构,接下来我们创建新的 Person 结构,然后把它加入到名为 people 的数组中.
// 创建一个新的Person:
Person evelyn = Person(16, "Evelyn");
// 将新创建的satoshi添加进people数组:
people.push(evelyn);
//也可以两步并一步,用一行代码更简洁:
people.push(Person(16, "Evelyn"));
注:array.push() 在数组的 尾部 加入新元素 ,所以元素在数组中的顺序就是我们添加的顺序
私有 / 公共函数
Solidity 定义的函数的属性默认为公共。 这就意味着任何一方 (或其它合约) 都可以调用你合约里的函数。
显然,不是什么时候都需要这样,而且这样的合约易于受到攻击。 所以将自己的函数定义为私有是一个好的编程习惯,只有当你需要外部世界调用它时才将它设置为公共。
定义一个私有的函数,在函数名字后面使用关键字 private 即可:
uint[] numbers;
function _addToArray(uint _number) private {
numbers.push(_number);
}
这意味着只有我们合约中的其它函数才能够调用这个函数,给 numbers 数组添加新成员。
函数的返回值和修饰符
要想函数返回一个数值,按如下定义:
string greeting = "What's up dog";
function sayHello() public returns (string) {
return greeting;
}
Solidity 里,函数的定义里可包含返回值的数据类型(如本例中 string)。
上面的函数实际上没有改变 Solidity 里的状态,即,它没有改变任何值或者写任何东西。
把函数定义为 view, 意味着它只能读取数据不能更改数据,
把函数定义为 pure , 表明这个函数甚至都不访问应用里的数据,它的返回值完全取决于它的输入参数
function sayHello() public view returns (string) {
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
散列函数Keccak256
散列函数keccak256,它用了SHA3版本。一个散列函数基本上就是把一个字符串转换为一个256位的16进制数字。字符串的一个微小变化会引起散列数据极大变化。
//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256("aaaab");
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256("aaaac");
事件
事件 是合约和区块链通讯的一种机制。你的前端应用“监听”某些事件,并做出反应。
// 这里建立事件
event IntegersAdded(uint x, uint y, uint result);
function add(uint _x, uint _y) public {
uint result = _x + _y;
//触发事件,通知app
IntegersAdded(_x, _y, result);
return result;
}
你的 app 前端可以监听这个事件。JavaScript 实现如下:
YourContract.IntegersAdded(function(error, result) {
// 干些事
})
边栏推荐
- Today, with the popularity of micro services, how does service mesh exist?
- 关于软件测试的一些思考
- Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- sqlilabs less-11~12
- MongoDB第二话 -- MongoDB高可用集群实现
- JVM performance tuning and practical basic theory part II
- 音乐播放器开发实例(可毕设)
- 【R语言数据科学】:机器学习常见评估指标
- Research Report on the development trend and competitive strategy of the global aviation leasing service industry
猜你喜欢

博文推荐 | 深入研究 Pulsar 中的消息分块
![[dynamic programming] interval dp:p1005 matrix retrieval](/img/c9/2091f51b905d2c0ebc978dab3d34d3.jpg)
[dynamic programming] interval dp:p1005 matrix retrieval

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words

643. Maximum average number of subarrays I
![[R language data science]: common evaluation indicators of machine learning](/img/c8/dbfb041fa72799fae1892fe8ac0050.png)
[R language data science]: common evaluation indicators of machine learning

C learning notes (5) class and inheritance

定了!2022海南二级造价工程师考试时间确定!报名通道已开启!

sqlilabs less-11~12

数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体

微服务大行其道的今天,Service Mesh是怎样一种存在?
随机推荐
炎炎夏日,这份安全用气指南请街坊们收好!
建立自己的网站(14)
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
Basis of target detection (NMS)
网速、宽带、带宽、流量三者之间的关系是什么?
【14. 区间和(离散化)】
Word2vec yyds dry goods inventory
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
What value can NPDP bring to product managers? Do you know everything?
Generate random numbers (4-bit, 6-bit)
Research Report on development trend and competitive strategy of global vibration polishing machine industry
sqlilabs less10
Pat 1065 a+b and C (64bit) (20 points) (16 points)
Cannot link redis when redis is enabled
保证生产安全!广州要求危化品企业“不安全不生产、不变通”
tensorflow2-savedmodel convert to tflite
Build your own website (14)
What are the requirements for NPDP product manager international certification registration?
SQLAchemy 常用操作
这3款在线PS工具,得试试