zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    ThinkPHP整合Kindeditor多图处理示例
    KindEditor用法介绍
    MySQL 1064 错误
    Nginx中虚拟主机与指定访问路径的设置方法讲解
    AJAX PHP无刷新form表单提交的简单实现(推荐)
    教PHP程序员如何找单位(全职+实习),超有用啊!
    利用正则表达式实现手机号码中间4位用星号(*)
    PHP项目做完后想上线怎么办,告诉你免费上线方法!
    备战NOIP——模板复习16
    备战NOIP——STL复习1
  • 原文地址:https://www.cnblogs.com/eilinge/p/9951268.html
Copyright © 2011-2022 走看看