『0008』- The use of public, internal and private in state variables and functions in Solidity and the inheritance and rewriting of Solidity smart contracts

Kongyi College: a leading brand of domestic blockchain vocational education

Author: Li Yuechun, blockchain and high-availability architecture engineer
WeChat: liyc1215 QQ group: 348924182 Blog: http://liyuechun.org

When we used this keyword in a function parameter in the previous section storage, the current function must be of type internalor private.

In this section, I (WeChat: liyc1215) will focus on introducing the usage permissions of attributes and functions .

Permissions of state variables and functions

1. public

Note: For the convenience of demonstration, I https://remix.ethereum.org/will demonstrate directly.

publicType state variables and functions have the greatest permissions and can be accessed externally, sub-contracts, and within the contract.

pragma solidity ^0.4.4;

contract Animal {


    string _birthDay; // 生日
    int public _age; // 年龄
    int internal _weight; // 身高
    string private _name; // 姓名


    function Animal() {
      _age = 29;
      _weight = 170;
      _name = "Lucky dog";
      _birthDay = "2011-01-01";
    }

    function birthDay() constant returns (string) {
      return _birthDay;
    }

    function age() constant public returns (int) {
      return _age;
    }

    function height() constant internal returns (int) {
      return _weight;
    }

    function name() constant private returns (string) {
      return _name;
    }

}

solidity public

In this contract, we can easily see from the running results that there are three functions that can be called externally, namely, birthDay, _age. ageSome people may ask, why can external functions be called _age? Why can external functions be called _age? Why can external functions be called? As for calling _agefunctions,_age the reason is because the permissions of our state variables are that publicwhen the permissions of a state variable are type, it will automatically generate a function publicthat can be called externally . getIn our contract, because _ageit is publica type, there is actually a default one with the same name as the state variable in the contract get函数, as shown below:

function _age() constant public returns (int) {
  return _age;
}

Among the four functions we declare explicitly:

function birthDay() constant returns (string) {
    
    
  return _birthDay;
}

function age() constant public returns (int) {
    
    
  return _age;
}

function height() constant internal returns (int) {
    
    
  return _weight;
}

function name() constant private returns (string) {
    
    
  return _name;
}

From the above running results, we know that among these four functions, only the function birthDayand agethe function can be accessed externally. [PS: ageThe function is explicitly declared by me. _ageThe function is _ageautomatically publicgenerated because the state variable is of internaltype by default. Therefore, a function with the same name as the state variable that can be accessed by the outside will not be automatically generated] . In other words, only publicthe function of the type can be accessed by the outside. It can be seen that when the function is declared, it defaults to publicthe type, and the state variable is declared , the default is internaltype .

summary:

  • When a state variable is declared, it defaults to internaltype. Only publicstate variables explicitly declared as a type will automatically generate a function with the same name as the state variable getfor external acquisition of the value of the current state variable.
  • When a function is declared, it defaults to publica type, and publicis accessible to the outside just like a function explicitly declared as a type.
2. internal
  • internalType state variables are available for external and sub-contract calls.
  • internalType functions are the same as privatetype functions. Smart contracts call them internally. They are protectednot exactly the same as those in other languages.
pragma solidity ^0.4.4;

contract Animal {

    string _birthDay; // 生日
    int public _age; // 年龄
    int internal _weight; // 身高
    string private _name; // 姓名

    function Animal() {
    
    
      _age = 29;
      _weight = 170;
      _name = "Lucky dog";
      _birthDay = "2011-01-01";
    }

    function birthDay() constant returns (string) {
    
    
      return _birthDay;
    }

    function age() constant public returns (int) {
    
    
      return _age;
    }

    function height() constant internal returns (int) {
    
    
      return _weight;
    }

    function name() constant private returns (string) {
    
    
      return _name;
    }

}


contract Person is Animal {

    function Person() {
    
    

        _age = 50;
        _weight = 270;
        _birthDay = "2017-01-01";

    }
}

solidity contract inheritance

In this case, contract Person is Animalthe Personcontract inherits all the state variables Animalof the contract public/internal, but can only inherit all publictypes of functions in the parent contract, functions that cannot be inherited internal/private, functions that cannot be inherited , and functions internal/privatethat cannot be inherited .internal/private

3. private

When we persontry to call _namestate variables in the contract, you will find that the compilation cannot pass.

solidity private

Because _namestate variables are private types in Animalthe contract and can only be used internally, an error will be reported when we try to use them in the sub-contract .privateAnimalPerson

4. Rewrite

A child contract can inherit functions of the type of the parent contract and can only inherit functions of the public type. We can directly call the inherited functions. Of course, we can also Inherited functions are rewritten.public

  • Before rewriting
pragma solidity ^0.4.4;

contract Animal {

    string _birthDay; // 生日
    int public _age; // 年龄
    int internal _weight; // 身高
    string private _name; // 姓名

    function Animal() {
      _age = 29;
      _weight = 170;
      _name = "Lucky dog";
      _birthDay = "2011-01-01";
    }

    function birthDay() constant returns (string) {
      return _birthDay;
    }

    function age() constant public returns (int) {
      return _age;
    }

    function height() constant internal returns (int) {
      return _weight;
    }

    function name() constant private returns (string) {
      return _name;
    }

}


contract Person is Animal {

}

solidity before rewriting

  • After rewriting
pragma solidity ^0.4.4;

contract Animal {

    string _birthDay; // 生日
    int public _age; // 年龄
    int internal _weight; // 身高
    string private _name; // 姓名

    function Animal() {
    
    
      _age = 29;
      _weight = 170;
      _name = "Lucky dog";
      _birthDay = "2011-01-01";
    }

    function birthDay() constant returns (string) {
    
    
      return _birthDay;
    }

    function age() constant public returns (int) {
    
    
      return _age;
    }

    function height() constant internal returns (int) {
    
    
      return _weight;
    }

    function name() constant private returns (string) {
    
    
      return _name;
    }

}


contract Person is Animal {

    function birthDay() constant returns (string) {
    
    

      return "2020-12-15";
    }

}

solidity after rewriting

summary

This article mainly comprehensively introduces public、internal、privatethe application of the three permissions in the contract's state variables and functions in the internal, external and sub-contracts of the contract. By studying this tutorial, I believe you will learn more about state variable inheritance and function inheritance and rewriting . In the next series of articles, we will further teach the relevant syntax in Solidity and the precautions during development.

Technology Exchange

  • Blockchain technology exchange QQ group: 348924182

  • "Blockchain Tribe" official public account

Guess you like

Origin blog.csdn.net/liyuechun520/article/details/78408608#comments_18893023