可靠性 "Function needs to specify overridden contract" 问题

Solidity "Function needs to specify overridden contract" question

我是Solidity的新手,我有一个关于多重继承的问题。

所以如果我有这样的合同:

contract A {

    function foo() public virtual {
        console.log("A");
    }
}

contract B is A {
    function foo() public virtual override {
        console.log("B");
    }
}

contract C is A, B {
    function foo() public override(A, B) {
        super.foo();
    }
}

合约C的foo函数必须override(A, B) insdead override(B)

否则它会抛出类似 Function needs to specify overridden contract "A".

的错误

那么问题来了,函数必须指定完全继承父类。

为什么contract C is A, B,

无法知道信息

我的意思是,这有什么意义? overrider(A,B) 部分是不必要的。

或者有什么技巧我不会?

请给我一个答案,很好奇,无法通过文档找到一些有用的信息。

覆盖意味着您正在重新实现您继承的方法!。因此,当您在子 class 中调用此函数时,将调用此函数的重新实现版本。

如果您只是调用该函数而不在子 class 内部重新实现,您可以调用它,因为此代码:

   contract B is A {}

如果调用 B 中未定义的方法,编译器将检查合约 A 并发现它会调用它。

solidity的官方文档说:

For multiple inheritance, the most derived base contracts that define the same function must be specified explicitly after the override keyword. In other words, you have to specify all base contracts that define the same function and have not yet been overridden by another base contract (on some path through the inheritance graph). Additionally, if a contract inherits the same function from multiple (unrelated) bases, it has to explicitly override it

因此在您的示例中,您必须使用 explicit override 关键字。