• ethereum(以太坊)(二)--合约中属性和行为的访问权限


    pragma solidity ^0.4.0;
    
    contract Test{
        /* 属性的访问权限
        priveta public internal
        defualt internal
        interlnal,private cannot be accessed from outside
        */
        uint8 internal _money;
        uint8 _age;
        uint8 private _height;
        uint8 public _sex;
        
        /*uint8 public _sex == function _sex() returns(uint8) 
        当属性类型为public时,会生成一个和属性名相同并且返回值就是当前属性的get函数
        function _sex()函数会覆盖掉public类型的属性自动生成的get函数;_sex()返回的是1,而不是0
        */
        function _sex() returns(uint8){
            //return _sex;
            //this._age; return this.age();TypeError: Member "age" not found or not visible after argument-dependent lookup in contract Test
            this._sex();
            return 1;
        }
        
        /*方法的访问权限
        //public private internal external
        //defualt public
        //internal private cannot be accessed from outside
        */
        function sex() returns(uint8){
            return 1;
        }
        function age() internal returns(uint8){
            return 18;
        }
        function height() public returns(uint8){
            return 175;
        }
        function money() private returns(uint){
            return 3500;
        }
        
        function year() external returns(uint){
            return 2018;
        }
        
        function testInternal() returns(uint){
            //return this.sex();
            //return this.year(); this(指针) 通过合约地址进行访问 
            return money();
            //return this.age();TypeError: Member "age" not found or not visible after argument-dependent lookup in contract Person
            //
        }
    }
    无论是属性还是方法,只有是public/external类型时,才可以通过合约地址进行访问,合约内部的this就是当前合约地址。
    在合约内部如果要访问internal,private类型的属性或者是函数,直接访问即可,不要试图通过this去访问
    //本合约内部调用
    //this.external调用 
    function test() public{
        getC(); //interl
        this.getD();//external
        getE();//private
        this.kill();//public
        kill();//public
    }
    
    //未使用继承,外部调用
    //只能调用external/public
    contract mytract{
    
    function getA() public {
        **getC(); //interl
        this.getD();//external
        **getE();//private
        this.kill();//public
        **kill();//public
        }
      }
    
    //使用继承,外部调用
    //无法调用私有变量
    contract mytract is Funder{
        
        function getA() public {
            getC(); //interl
            this.getD();//external
            **getE();//private
            this.kill();//public
            kill();//public
        }
    }
  • 相关阅读:
    使用Mutex实现程序单实例运行(c#)
    KMP(转载来自Matrix67原创)
    【转载】搞ACM的你伤不起(转载,不过这个神作实在是太经典了)
    POJ 3125 Printer Queue【打印队列】
    弱校ACM奋斗史
    POJ 2063 Investment
    程序员的艺术:排序算法舞蹈【视频】
    POJ 2063 Investment【经典完全背包】
    快速幂模板
    搞ACM的你伤不起(转载,不过这个神作实在是太经典了)
  • 原文地址:https://www.cnblogs.com/eilinge/p/9951268.html
Copyright © 2020-2023  润新知