当前位置:网站首页>Solid basic basic grammar and definition function

Solid basic basic grammar and definition function

2022-07-01 14:49:00 new life1937

Basic grammar

contract

Solidity The code is wrapped in the contract . A contract is the basic module of etheric Application , All variables and functions belong to a contract , It's the starting point for all your apps .

State variables and integers

State variables are permanently stored in the contract . In other words, they are written into the etheric blockchain . Imagine writing to a database .
uint The unsigned data type , Means that its value cannot be negative ,
int For data types where signed integers exist .
string String is used to hold any length of UTF-8 Encoding data

Mathematical operations

stay Solidity in , The mathematical operation is very intuitive , Same as other programming languages :
Add : x + y
Subtraction : x - y,
Multiplication : x * y
division : x / y
modulus / Seeking remainder : x % y ( for example , 13 % 5 more than 3, because 13 Divide 5, more than 3)
Solidity And support Power operation ( Such as :x Of y Power ) // for example : 5 ** 2 = 25

Structure - More complex data types , There are multiple attributes

struct Structs allow you to generate a more complex data type , It has multiple attributes .

struct Person {
  uint age;
  string name;
}

Array

Build a collection , It can be used Array _ This type of data . Solidity Two arrays are supported : static state Array and _ dynamic Array :

//  The fixed length is 2 Static array of :
uint[2] fixedArray;
//  The fixed length is 5 Of string Static array of type :
string[5] stringArray;
//  The dynamic array , Length is not fixed , You can add elements dynamically :
uint[] dynamicArray;

An array of struct types , The state variables are permanently stored in the blockchain .

Person[] people; //  This is a dynamic array , We can keep adding elements 
Public array

You can define public Array , Solidity Automatically created getter Method . The grammar is as follows :

Person[] public people;

Other contracts can read data from this array ( But you can't write data ), So it's a useful model for holding public data in contracts .

Defined function

stay Solidity The syntax of the function definition in is as follows :

function eatHamburgers(string _name, uint _amount) {

}

This one is called eatHamburgers Function of , It takes two arguments : One string Type of and One uint Type of . Now the function is still empty .
Our function is defined as follows :

eatHamburgers("evelyn", 100);
原网站

版权声明
本文为[new life1937]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011446084817.html