簡單代碼:
pragma solidity 0.8.4;
contract A {
uint256 public constant X = 1;
}
contract B is A {
uint256 override public constant X = 2;
}
不幸的是,編譯錯誤:
TypeError: Cannot override public state variable.
--> contracts/mocks/StakePoolMock.sol:4:5:
|
4 | uint256 public constant X = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note: Overriding public state variable is here:
--> contracts/mocks/StakePoolMock.sol:8:5:
|
8 | uint256 override public constant X = 2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
有沒有辦法覆寫公共常量?
uj5u.com熱心網友回復:
常量變數的值在編譯程序中被賦值,然后存盤在記憶體中。不幸的是你不能改變它。
我不知道X您代碼中的目的,但是可以輕松地重新分配簡單的狀態變數,并且通過使用 onlyOwner 修飾符,您將擁有對該函式的獨占訪問權限,因此您可以隨時更改該變數。
function changeValue(uint256 newValue) public onlyOwner {
X = newValue;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/356616.html
上一篇:顯示資料中的PHP字串問題
