Access to properties in solidity contracts

Access to properties in solidity contracts

Attributes: state variables.

Ethereum has 4 visibility types/access rights: public, private, internal, external

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

contract Person {
  uint internal _age;
  uint private _height;
  uint public _weight;    
  
  function _weight() constant returns (uint) {
    return 120; 
  }
}

illustrate:

  • The internal type is the default attribute of the contract.

  • Attributes of type internal and private cannot be accessed externally.

  • When the attribute type is public, a get (that is, read) function with the same name as the attribute will be generated, and the return value is the current attribute.

    The get function written by myself will overwrite the get function automatically generated by the property of the public type.

In the picture above, the return value of the _monery() function is 120, not 0.

Guess you like

Origin blog.csdn.net/qq_44909497/article/details/124211100