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
        }
    }
  • 相关阅读:
    input搜索框实时检索功能实现(超简单,核心原理请看思路即可)
    django blank 和autonow
    dwebsocket的坑
    vue 动态添加active+父子传值
    NO 2,人生苦短,我学python之python+selenium元素定位
    NO 1,人生苦短,我学python之python+selenium自动化环境搭建
    SPU与SKU概念
    数据库,缓存数据一致性常用解决方案总结
    利用注解 + 反射消除重复代码
    Nacos学习与实战
  • 原文地址:https://www.cnblogs.com/eilinge/p/9951268.html
Copyright © 2011-2022 走看看