Solidity-026 ScopingStateVariables

发布时间:2024年01月24日

// 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:


文章来源:https://blog.csdn.net/m0_61408373/article/details/135830911
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。