如需获取本内容的最新版本,请参见 Cyfrin.io 上的Immutable(代码示例)
不可变变量就像常数。不可变变量的值可以在构造函数内设置,但之后无法修改。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract Immutable {
address public immutable myAddr;
uint256 public immutable myUint;
// 构造函数
constructor(uint256 _myUint) {
myAddr = msg.sender;
myUint = _myUint;
}
}
Remix Lite 尝试一下
END