子合约可以更改 Solidity 中父合约的值吗
Can a Child contract change values of a parent contract in Solidity
如果我们有 2 个这样的合同:
contract A {
struct SampleA{
uint id;
bytes32 name;
bytes32 toChange;
}
mapping (uint=> SampleA) public idToStruct;
}
contract B is A{
function changeVar (bytes32 newVar) public {
idToStruct[0].toChange = newVar;
}
}
我可以像这样从合同B更新合同A中的变量吗?
这在 solidity 中是否可行,如果不能,是否有解决方法?
是的,您可以修改父合同的属性,只要 属性 不是 private
。
文档:https://docs.soliditylang.org/en/v0.8.6/contracts.html#visibility-and-getters
如果我们有 2 个这样的合同:
contract A {
struct SampleA{
uint id;
bytes32 name;
bytes32 toChange;
}
mapping (uint=> SampleA) public idToStruct;
}
contract B is A{
function changeVar (bytes32 newVar) public {
idToStruct[0].toChange = newVar;
}
}
我可以像这样从合同B更新合同A中的变量吗? 这在 solidity 中是否可行,如果不能,是否有解决方法?
是的,您可以修改父合同的属性,只要 属性 不是 private
。
文档:https://docs.soliditylang.org/en/v0.8.6/contracts.html#visibility-and-getters