solidity基础语法

Posted 泠泠在路上

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solidity基础语法相关的知识,希望对你有一定的参考价值。

//默认值
contract DefaultValuesFunctions
bool public b;// false
uint public u;//0
int public i;//0
address public a;//0x0000000000000000000000000000000000000000
bytes32 public b32;// 0x0000000000000000000000000000000000000000000000000000000000000000

//如果不需要修改,想要变成常量,就加constant,变量名大写,如:bool public constant B;


//判断控制
contract IfElseFunctions
function example(uint x) external pure returns(uint)
    if(x<10)
        return 1;
    else if(x<20)
        return 2;
    else
        return 3;
    


function ternary(uint x) external pure returns(uint)
return x<10?1:2;




//for循环
contract Loop 
    function loop() public 
        // for loop
        for (uint i = 0; i < 10; i++) 
            if (i == 3) 
                // Skip to next iteration with continue
                continue;
            
            if (i == 5) 
                // Exit loop with break
                break;
            
        

        // while loop
        uint j;
        while (j < 10) 
            j++;
        
    


//数组
contract Array 
    // Several ways to initialize an array
    uint[] public arr;
    uint[] public arr2 = [1, 2, 3];
    // Fixed sized array, all elements initialize to 0
    uint[10] public myFixedSizeArr;

    function get(uint i) public view returns (uint) 
        return arr[i];
    

    // Solidity can return the entire array.
    // But this function should be avoided for
    // arrays that can grow indefinitely in length.
    function getArr() public view returns (uint[] memory) 
        return arr;
    

    function push(uint i) public 
        // Append to array
        // This will increase the array length by 1.
        arr.push(i);
    

    function pop() public 
        // Remove last element from array
        // This will decrease the array length by 1
        arr.pop();
    

    function getLength() public view returns (uint) 
        return arr.length;
    

    function remove(uint index) public 
        // Delete does not change the array length.
        // It resets the value at index to it's default value,
        // in this case 0
        delete arr[index];
    

    function examples() external 
        // create array in memory, only fixed size can be created
        uint[] memory a = new uint[](5);
    

//数组的删除元素
contract ArrayRemoveByShifting 
    // [1, 2, 3] -- remove(1) --> [1, 3, 3] --> [1, 3]
    // [1, 2, 3, 4, 5, 6] -- remove(2) --> [1, 2, 4, 5, 6, 6] --> [1, 2, 4, 5, 6]
    // [1, 2, 3, 4, 5, 6] -- remove(0) --> [2, 3, 4, 5, 6, 6] --> [2, 3, 4, 5, 6]
    // [1] -- remove(0) --> [1] --> []

    uint[] public arr;

    function remove(uint _index) public 
        require(_index < arr.length, "index out of bound");

        for (uint i = _index; i < arr.length - 1; i++) 
            arr[i] = arr[i + 1];
        
        arr.pop();
    

    function test() external 
        arr = [1, 2, 3, 4, 5];
        remove(2);
        // [1, 2, 4, 5]
        assert(arr[0] == 1);
        assert(arr[1] == 2);
        assert(arr[2] == 4);
        assert(arr[3] == 5);
        assert(arr.length == 4);

        arr = [1];
        remove(0);
        // []
        assert(arr.length == 0);
    

//映射
contract Mapping 
    // Mapping from address to uint
    mapping(address => uint) public myMap;

    function get(address _addr) public view returns (uint) 
        // Mapping always returns a value.
        // If the value was never set, it will return the default value.
        return myMap[_addr];
    

    function set(address _addr, uint _i) public 
        // Update the value at this address
        myMap[_addr] = _i;
    

    function remove(address _addr) public 
        // Reset the value to the default value.
        delete myMap[_addr];
    


contract NestedMapping 
    // Nested mapping (mapping from address to another mapping)
    mapping(address => mapping(uint => bool)) public nested;

    function get(address _addr1, uint _i) public view returns (bool) 
        // You can get values from a nested mapping
        // even when it is not initialized
        return nested[_addr1][_i];
    

    function set(
        address _addr1,
        uint _i,
        bool _boo
    ) public 
        nested[_addr1][_i] = _boo;
    

    function remove(address _addr1, uint _i) public 
        delete nested[_addr1][_i];
    



以上是关于solidity基础语法的主要内容,如果未能解决你的问题,请参考以下文章

区块链特辑——solidity语言基础

区块链特辑——solidity语言基础

区块链开发之Solidity编程基础

区块链开发之Solidity编程基础

区块链开发之Solidity编程基础

区块链开发之Solidity编程基础合约数据存储