【Solidity学习笔记】public、private、internal、external区别

理解这几个词之前,我们可以把一个Contract类比成一个Class

Public: 修饰的变量和函数,合约内部函数和外部合约函数都能调用和访问, 类比C++中Class的Public属性

Private: 修饰的变量和函数,只能在其所在的合约中调用和访问,即使是其子合约也无法访问,类比C++中Class的Private属性

Internal: 和 Private类似,不过, 如果某个合约继承自其父合约,这个合约即可以访问父合约中定义的Internal函数,类比C++中Class的Protected属性

External: 与Public类似,不过这些函数只能在合约外部被调用,不能被合约内的其他函数调用,纯粹的就是定义向外开放的接口,合约内部不能使用。但是采用this指针方式可以在合约内部被调用,如:

pragma solidity ^0.4.5;

contract FuntionTest{
    function externalFunc() external{}

    function callFunc(){
        //以`internal`的方式调用函数报错
        //Error: Undeclared identifier.
        //externalFunc();

        //以`external`的方式调用函数
        this.externalFunc();
    }
}

参考:https://www.jianshu.com/p/c3e3ccb466ec

猜你喜欢

转载自blog.csdn.net/ljlinjiu/article/details/82109249