Solidity-026 ScopingStateVariables

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract ScopingStateVariables {

   

    // Public state variable:

    // Accessible from within the contract, derived contracts, and externally.

    uint64 public myVarPublic = 0;

   

    // Private state variable:

    // Accessible only within this contract, not in derived ones or externally.

    uint64 private myVarPrivate = 0;

    // Internal state variable:

    // Accessible within this contract and in derived contracts, but not externally.

    uint64 internal myVarInternal = 0;

    // Function to increment the public variable.

    // Demonstrates internal access to a public variable.

    function incrementPublicVar() public {

        myVarPublic++;

    }

    // Function to increment the private variable.

    // Can only be called within this contract.

    function incrementPrivateVar() public {

        myVarPrivate++;

    }

    // Function to increment the internal variable.

    // Can be called within this contract and derived contracts.

    function incrementInternalVar() public {

        myVarInternal++;

    }

    // Function to get the current value of the private variable.

    // Private variables do not have auto-generated getter functions,

    // so we create a public function to allow external access.

    function getPrivateVar() public view returns (uint64) {

        return myVarPrivate;

    }

    // Function to get the current value of the internal variable.

    // Like private, internal variables do not have auto-generated getter functions.

    // This function allows reading the internal variable's value externally.

    function getInternalVar() public view returns (uint64) {

        return myVarInternal;

    }

}

//Deploy show:


Solidity-026 ScopingStateVariables_第1张图片

你可能感兴趣的:(Solidity,金融,区块链,智能合约,分布式账本,信任链)