第4节:语法-变量
小白入门:https://github.com/dukedaily/solidity-expert ,欢迎star转发,文末加V入群。
职场进阶: https://dukeweb3.com
以太坊一共有三种类型的变量
- 状态变量(state)- 定义在合约内,函数外
- 存储在链上
 
- 本地变量(local)- 定义在函数内
- 不会存储在链上
 
- 全局变量(global)- 与当前合约无关,描述整个区块链的信息(时间、块高等)
 
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Variables {
    // State variables are stored on the blockchain.
    string public msg = "Hello";
    uint public age = 26;
    function test() public {
        // Local variables are not saved to the blockchain.
        uint i = 456;
        // Here are some global variables
        uint height = block.blocks; // Current block height
        address sender = msg.sender; // address of the caller
    }
}