其他分享
首页 > 其他分享> > Solidity0.8-Shadowing Inherited State Variables

Solidity0.8-Shadowing Inherited State Variables

作者:互联网

Unlike functions, state variables cannot be overridden by re-declaring it in the child contract.

Let's learn how to correctly override inherited state variables.

 
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract A {
    string public name = "Contract A";

    function getName() public view returns (string memory) {
        return name;
    }
}

// Shadowing is disallowed in Solidity 0.6
// This will not compile
// contract B is A {
//     string public name = "Contract B";
// }

contract C is A {
    // This is the correct way to override inherited state variables.
    constructor() {
        name = "Contract C";
    }

    // C.getName returns "Contract C"
}

标签:name,Variables,state,contract,public,State,Contract,Solidity0.8,variables
来源: https://www.cnblogs.com/zaleswift/p/16538411.html