zoukankan      html  css  js  c++  java
  • solidity合约面向对象

    1. 属性【状态变量】的访问权限

    public  internal【合约属性默认的权限】  private

       说明:属性默认访问全向为internalinternalprivate类型的属性,外部是访问不到的。

    当属性为public时,会自动生成一个和属性同名并且返回值为当前属性的get方法。

      例如:

    uint public _age;

    会自动生成一个:

    function _age() constant  returns (uint){
        return _age;
    }

      如果自己定义了_age方法,会覆盖原来的方法:

    internal:

    public:

    2.行为【合约的方法】的访问权限

    访问权限有:public internal private

      方法默认为public类型。

     3.继承

    this指针只能访问public类型的成员,只有是public类型,才能通过合约地址访问【合约内部的this,就是当前合约的地址】,在合约内部如果要访问合约的internal和private,直接访问即可,不要使用this。

      继承使用关键字is:

    pragma solidity ^0.4.4;
    
    contract Animal{
      uint _weight;
      uint public _age;
      uint private _name;
      
      function test() constant {
          
      }
    }
    
    contract Dog is Animal{
      
    }

     

      对于public类型的成员,子类直接继承过来;

      对于internal类型的成员,子类在内部可以访问;

      对于private类型的成员,子类不能直接访问;

    多继承:is Animal, Eat

    pragma solidity ^0.4.4;
    
    contract Animal{
      uint _weight;
      uint public _age;
      uint private _name;
      
      function test() constant returns (uint){
          return 100;
      }
    }
    
    contract Eat{
      function eat() constant returns(string){
        return "eat";
      }
    }
    
    contract Dog is Animal,Eat{
        
        function test() constant returns (uint){
          return 200;
      }
        
        function getWeight() constant returns (uint){
            return _weight;
        }
      
    }
    View Code

     4.值类型,引用类型

    值类型有:

    • Boolean
    • Integer
    • Address
    • fixed byte array【定长字节数组】
    • rational and integer literals ,string linterals【有理数和整形】
    • enum【枚举】
    • function【函数】

     引用类型有:

    • 不定长字节数组
    • 字符串
    • 数组
    • 结构体

       函数参数的默认引用类型为memory,即不会修改实参的值。当修改为storage时,可以修改引用类型的变量的值,如果函数使用了storage,那么只能是internal【只能内部调用】.

      对于字符串,只能修改单个字符。bytes(name)[0] = 'L'   【索引不能出界】

    pragma solidity ^0.4.4;
    
    contract Person{
      string public _name;
      function Person(string name){
          _name=name;
      }
      
      function f(){
          modify(_name);
      }
      
      function modify(string storage name) internal {
          bytes(name)[0] = 'A';
      }
      
      function name() constant returns (string){
          return _name;
      }
    }
    View Code
  • 相关阅读:
    C# 从服务器下载文件
    不能使用联机NuGet 程序包
    NPOI之Excel——合并单元格、设置样式、输入公式
    jquery hover事件中 fadeIn和fadeOut 效果不能及时停止
    UVA 10519 !! Really Strange !!
    UVA 10359 Tiling
    UVA 10940 Throwing cards away II
    UVA 10079 Pizze Cutting
    UVA 763 Fibinary Numbers
    UVA 10229 Modular Fibonacci
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/9461159.html
Copyright © 2011-2022 走看看