// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;
contract A {
event Log(string message);
function bar() public virtual {
emit Log("A.bar called");
}
}
contract B is A {
function bar() public virtual override {
emit Log("B.bar called");
super.bar();
}
}
contract C is A {
function bar() public virtual override {
emit Log("C.bar called");
super.bar();
}
}
contract D is B, C {
function bar() public override(B, C) {
super.bar();
}
}
执行D.bar的结果
[
{
"from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8",
"topic": "0xcf34ef537ac33ee1ac626ca1587a0a7e8e51561e5514f8cb36afa1c5102b3bab",
"event": "Log",
"args": {
"0": "C.bar called",
"message": "C.bar called"
}
},
{
"from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8",
"topic": "0xcf34ef537ac33ee1ac626ca1587a0a7e8e51561e5514f8cb36afa1c5102b3bab",
"event": "Log",
"args": {
"0": "B.bar called",
"message": "B.bar called"
}
},
{
"from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8",
"topic": "0xcf34ef537ac33ee1ac626ca1587a0a7e8e51561e5514f8cb36afa1c5102b3bab",
"event": "Log",
"args": {
"0": "A.bar called",
"message": "A.bar called"
}
}
]
执行顺序
- 最右C.bar
- C里有 super.bar,执行的是b里的 bar
- B里的 super.bar 执行的是a里的bar
具体原因是:
Solidity Diamond Inheritance - Guides and Tutorials - OpenZeppelin Forum
solidity和python一样采用C3 继承算法文章来源:https://www.toymoban.com/news/detail-518757.html
去掉代码里的 super.bar,结果就变成文章来源地址https://www.toymoban.com/news/detail-518757.html
[
{
"from": "0xd2a5bC10698FD955D1Fe6cb468a17809A08fd005",
"topic": "0xcf34ef537ac33ee1ac626ca1587a0a7e8e51561e5514f8cb36afa1c5102b3bab",
"event": "Log",
"args": {
"0": "C.bar called",
"message": "C.bar called"
}
}
]
到了这里,关于Solidity 多重继承 C3算法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!