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
        }
    }
  • 相关阅读:
    读写分离
    java并发集合知识点(二)
    jdbc框架有很多,包括spring jdbc
    多线程,势必涉及到共享对象读写问题
    <xliff:g>标签
    租赁市场的上海方(浦东/张江)
    HDU 3488Tour(流的最小费用网络流)
    sql使用存储过程和交易
    状态压缩动态规划 -- 骨牌
    Android-2手机应用程序,短信应用
  • 原文地址:https://www.cnblogs.com/eilinge/p/9951268.html
Copyright © 2011-2022 走看看