当前位置:网站首页>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) {
// 干些事
})
边栏推荐
- Research Report on development trend and competitive strategy of global consumer glassware industry
- WebSocket(简单体验版)
- Provincial election + noi Part VIII fraction theory
- En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
- SQLAchemy 常用操作
- Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
- sqlilabs less-11~12
- MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
- Leetcode (69) -- square root of X
- MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
猜你喜欢

In hot summer, please put away this safe gas use guide!

JVM performance tuning and practical basic theory part II

关于重载运算符的再整理
![[leetcode 324] 摆动排序 II 思维+排序](/img/cb/26d89e1a1f548b75a5ef9f29eebeee.png)
[leetcode 324] 摆动排序 II 思维+排序

【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较

Summary of leetcode's dynamic programming 5

Minimum spanning tree and bipartite graph in graph theory (acwing template)

Buuctf reinforcement question ezsql

How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?

JVM second conversation -- JVM memory model and garbage collection
随机推荐
[leetcode 324] swing sorting II thinking + sorting
QT capture interface is displayed as picture or label
Pat 1065 a+b and C (64bit) (20 points) (16 points)
NPDP产品经理国际认证报名有什么要求?
炎炎夏日,这份安全用气指南请街坊们收好!
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
JVM performance tuning and practical basic theory part II
Word2vec yyds dry goods inventory
sqlilabs less-8
三十之前一定要明白的职场潜规则
One of the first steps to redis
WebSocket(简单体验版)
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
Day-02 database
Minimum spanning tree and bipartite graph in graph theory (acwing template)
sqlilabs less13
About the use of HTTP cache validation last modified and Etag
111. Minimum depth of binary tree