Solidity中的Immutable变量

232 阅读1分钟

Immutable变量和常量类似,它们可以在构造函数中被赋值,但是在那之后,它们的值就不可以再被修改了。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Immutable {
    // 一般使用全大写来表示immutable变量
    address public immutable MY_ADDRESS;
    uint public immutable MY_UINT;

    constructor(uint _myUint) {
        MY_ADDRESS = msg.sender;
        MY_UINT = _myUint;
    }
}