0

我想从Ownable覆盖 transferOwnership但是,内部函数 _transferOwnership 未被识别。我要么必须有办法调用它,要么直接更改 _transferOwnership。

function transferOwnership(address newOwner) public virtual onlyOwner {
    require(newOwner != address(0), "Ownable: new owner is the zero address");
    _transferOwnership(newOwner);
}
4

1 回答 1

0

您唯一缺少的是override关键字。

文档:https ://docs.soliditylang.org/en/v0.8.9/contracts.html#function-overriding

pragma solidity ^0.8;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract MyContract is Ownable {
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        // TODO your own implementation
    }
}

注意:virtual如果您不希望/不需要此函数被进一步覆盖(通过派生自 的合约MyContract),您也可以删除关键字。

于 2021-10-22T08:51:48.127 回答