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:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值