当前位置:网站首页>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) {
// 干些事
})
边栏推荐
- C#学习笔记(5)类和继承
- Pat 1065 a+b and C (64bit) (20 points) (16 points)
- 建立自己的网站(14)
- Minimum spanning tree and bipartite graph in graph theory (acwing template)
- Problem note - Oracle 11g uninstall
- Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
- Vnctf2022 open web gocalc0
- 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é
- Websocket (simple experience version)
猜你喜欢
![[14. Interval sum (discretization)]](/img/e5/8b29aca7068a6385e8ce90c2742c37.png)
[14. Interval sum (discretization)]

sqlilabs less10

sqlilabs less-8

Leetcode(69)——x 的平方根

Build your own website (14)

2022-2-15 learning the imitation Niuke project - Section 3 post details

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?

Fundamentals of C language

炎炎夏日,这份安全用气指南请街坊们收好!
随机推荐
Research Report on the development trend and competitive strategy of the global display filter industry
Fundamentals of C language
Music player development example (can be set up)
MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
NPDP能给产品经理带来什么价值?你都知道了吗?
Take you to API development by hand
tensorflow2-savedmodel convert to pb(frozen_graph)
问题随记 —— Oracle 11g 卸载
从零开发小程序和公众号【第三期】
JVM performance tuning and practical basic theory part II
sqlilabs less9
Is the futures company found on Baidu safe? How do futures companies determine the regularity
建立自己的网站(14)
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
[dynamic programming] interval dp:p1005 matrix retrieval
Provincial election + noi Part IX game theory
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
What problems should be considered for outdoor LED display?
241. 为运算表达式设计优先级