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 internal
or 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.
public
Type 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;
}
}
In this contract, we can easily see from the running results that there are three functions that can be called externally, namely, birthDay
, _age
. age
Some 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 _age
functions,_age
the reason is because the permissions of our state variables are that public
when the permissions of a state variable are type, it will automatically generate a function public
that can be called externally . get
In our contract, because _age
it is public
a 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 birthDay
and age
the function can be accessed externally. [PS: age
The function is explicitly declared by me. _age
The function is _age
automatically public
generated because the state variable is of internal
type 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 public
the function of the type can be accessed by the outside. It can be seen that when the function is declared, it defaults to public
the type, and the state variable is declared , the default is internal
type .
summary:
- When a state variable is declared, it defaults to
internal
type. Onlypublic
state variables explicitly declared as a type will automatically generate a function with the same name as the state variableget
for external acquisition of the value of the current state variable. - When a function is declared, it defaults to
public
a type, andpublic
is accessible to the outside just like a function explicitly declared as a type.
2. internal
internal
Type state variables are available for external and sub-contract calls.internal
Type functions are the same asprivate
type functions. Smart contracts call them internally. They areprotected
not 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";
}
}
In this case, contract Person is Animal
the Person
contract inherits all the state variables Animal
of the contract public/internal
, but can only inherit all public
types of functions in the parent contract, functions that cannot be inherited internal/private
, functions that cannot be inherited , and functions internal/private
that cannot be inherited .internal/private
3. private
When we person
try to call _name
state variables in the contract, you will find that the compilation cannot pass.
Because _name
state variables are private types in Animal
the contract and can only be used internally, an error will be reported when we try to use them in the sub-contract .private
Animal
Person
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 {
}
- 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";
}
}
summary
This article mainly comprehensively introduces public、internal、private
the 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