当前位置:网站首页>Solid basic structure and array, private / public function, return value and modifier of function, event
Solid basic structure and array, private / public function, return value and modifier of function, event
2022-07-01 14:48:00 【new life1937】
Use structs and arrays
Create a new structure
struct Person {
uint age;
string name;
}
Person[] public people;
This is Person structure , Next, we create a new Person structure , Then add it to a file called people In the array .
// Create a new Person:
Person evelyn = Person(16, "Evelyn");
// Will create a new satoshi Added to the people Array :
people.push(evelyn);
// You can also take two steps at a time , Simpler with one line of code :
people.push(Person(16, "Evelyn"));
notes :array.push() In the array The tail Add new elements , So the order of elements in the array is the order we add
private / Public function
Solidity The properties of defined functions are public by default . That means either side ( Or other contracts ) You can call the functions in your contract .
obviously , It's not always necessary , And such contracts are vulnerable . So it's a good programming habit to define your own functions as private , Make it public only when you need the outside world to call it .
Define a private function , Use the keyword... After the function name private that will do :
uint[] numbers;
function _addToArray(uint _number) private {
numbers.push(_number);
}
This means that only other functions in our contract can call this function , to numbers Array add new members .
Return value and modifier of function
To return a value from a function , As defined below :
string greeting = "What's up dog";
function sayHello() public returns (string) {
return greeting;
}
Solidity in , The definition of the function can contain the data type of the return value ( In this case string).
The above function has not actually changed Solidity State in , namely , It doesn't change any value or write anything .
Define the function as view, This means that it can only read data and cannot change data ,
Define the function as pure , Indicates that this function does not even access the data in the application , Its return value depends entirely on its input parameters
function sayHello() public view returns (string) {
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
Hash function Keccak256
Hash function keccak256, It's used SHA3 edition . A hash function basically converts a string into a 256 Bit 16 Hexadecimal Numbers . A small change in the string will cause a great change in the hash data .
//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256("aaaab");
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256("aaaac");
event
event It is a mechanism for contract and blockchain communication . Your front end application “ monitor ” Some events , And react .
// Set up events here
event IntegersAdded(uint x, uint y, uint result);
function add(uint _x, uint _y) public {
uint result = _x + _y;
// Triggering event , notice app
IntegersAdded(_x, _y, result);
return result;
}
Yours app The front end can listen to this event .JavaScript The implementation is as follows :
YourContract.IntegersAdded(function(error, result) {
// Do something
})
边栏推荐
- JVM performance tuning and practical basic theory part II
- Mongodb second call -- implementation of mongodb high availability cluster
- Research Report on the development trend and competitive strategy of the global traditional computer industry
- Music player development example (can be set up)
- solidty-基础篇-基础语法和定义函数
- SQLAchemy 常用操作
- Microservice development steps (Nacos)
- 首届技术播客月开播在即
- Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
- 建立自己的网站(14)
猜你喜欢

互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码

MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology

Rearrangement of overloaded operators

In hot summer, please put away this safe gas use guide!
![[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion](/img/e1/d35e1d382e0e945849010941b219d3.png)
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion

JVM第二话 -- JVM内存模型以及垃圾回收

微服务开发步骤(nacos)

C learning notes (5) class and inheritance

Build your own website (14)

【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
随机推荐
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
定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
It's suitable for people who don't have eloquence. The benefits of joining the China Video partner program are really delicious. One video gets 3 benefits
Opencv mat class
博文推荐 | 深入研究 Pulsar 中的消息分块
适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
[dynamic programming] p1004 grid access (four-dimensional DP template question)
首届技术播客月开播在即
Fundamentals of C language
Details of appium key knowledge
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
Provincial election + noi Part VIII fraction theory
Problem note - Oracle 11g uninstall
保证生产安全!广州要求危化品企业“不安全不生产、不变通”
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
Ubuntu 14.04下搭建MySQL主从服务器
One of the first steps to redis
tensorflow2-savedmodel convert to tflite
基于价值量化的需求优先级排序方法
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models