当前位置:网站首页>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) {
// 干些事
})
边栏推荐
- 241. Design priorities for operational expressions
- [Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
- Mongodb second talk - - mongodb High available Cluster Implementation
- 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é
- 博文推荐 | 深入研究 Pulsar 中的消息分块
- Fundamentals of C language
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
- Error-tf.function-decorated function tried to create variables on non-first call
- 首届技术播客月开播在即
- Pat 1065 a+b and C (64bit) (20 points) (16 points)
猜你喜欢
博文推荐 | 深入研究 Pulsar 中的消息分块
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
Summary of leetcode's dynamic programming 5
What problems should be considered for outdoor LED display?
The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores
In hot summer, please put away this safe gas use guide!
Sqlachemy common operations
Semiconductor foundation of binary realization principle
Today, with the popularity of micro services, how does service mesh exist?
【R语言数据科学】:机器学习常见评估指标
随机推荐
111. Minimum depth of binary tree
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
【R语言数据科学】:机器学习常见评估指标
Is the futures company found on Baidu safe? How do futures companies determine the regularity
Leetcode(69)——x 的平方根
phpcms实现订单直接支付宝支付功能
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
[getting started with Django] 13 page Association MySQL "multi" field table (check)
[zero basic IOT pwn] reproduce Netgear wnap320 rce
[14. Interval sum (discretization)]
[R language data science]: common evaluation indicators of machine learning
【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
MongoDB第二話 -- MongoDB高可用集群實現
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
[zero basic IOT pwn] reproduce Netgear wnap320 rce
Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
About the use of HTTP cache validation last modified and Etag
定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
sqlilabs less-8