Solidity 智能合约开发语言·数据类型

  1. 云栖社区>
  2. 博客>
  3. 正文

Solidity 智能合约开发语言·数据类型

netkiller 2018-02-19 09:08:54 浏览1274
展开阅读全文

本文节选自电子书《Netkiller Blockchain 手札》

 

Netkiller Blockchain 手札

本文作者最近在找工作,有意向致电 13113668890

Mr. Neo Chan, 陈景峯(BG7NYT)


中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890

<netkiller@msn.com>

文档始创于2018-02-10

版权 © 2018 Netkiller(Neo Chan). All rights reserved.

 

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

by-nc-sa.png
http://www.netkiller.cn
http://netkiller.github.io
http://netkiller.sourceforge.net
weixin.jpg
微信订阅号 netkiller-ebook (微信扫描二维码)
QQ:13721218 请注明“读者”
QQ群:128659835 请注明“读者”

 

 

4.5. 数据类型

4.5.1. 数值型

int/uint:变长的有符号或无符号整型。变量支持的步长以8递增,支持从uint8到uint256,以及int8到int256。需要注意的是,uint和int默认代表的是uint256和int256。

有符号整型能够表示负数的代价是其能够存储正数的范围的缩小,因为其约一半的数值范围要用来表示负数。如:uint8的存储范围为0 ~ 255,而int8的范围为-127 ~ 127

支持的运算符:



比较:<=,<,==,!=,>=,>,返回值为bool类型。

位运算符:&,|,(^异或),(~非)。

数学运算:+,-,一元运算+,*,/,(%求余),(**次方),(<<左移),(>>右移)。
 

小数由"."组成,在他的左边或右边至少要包含一个数字。如"1.",".1","1.3"均是有效的小数。

4.5.1.1. 加 +,减 -,乘 *,除 / 运算演示

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Math {

  function mul(int a, int b) public pure returns (int) {

      int c = a * b;
      return c;
  }

  function div(int a, int b) public pure  returns (int) {

      int c = a / b;
      return c;
  }

  function sub(int a, int b) public pure  returns (int) {
      
      return a - b;
  }

  function add(int a, int b) public pure  returns (int) {

      int c = a + b;
      return c;
  }
}

4.5.1.2. 求余 %

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Math {

  function m(int a, int b) public pure returns (int) {

      int c = a % b;
      return c;
  }
}

4.5.1.3. 幂运算

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Math {

  function m(uint a, uint b) public pure returns (uint) {

      uint c = a**b;
      return c;
  }

}

4.5.1.4. 与 &,| 或,非 ~,^ 异或

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Math {

  function yu() public pure returns (uint) {

      uint a = 3; // 0b0011
      uint b = 4; // 0b0100
    
      uint c = a & b; // 0b0000
      return c; // 0
  }

  function huo() public pure returns (uint) {

      uint a = 3; // 0b0011
      uint b = 4; // 0b0100
    
      uint c = a | b; // 0b0111
      return c; // 7
  }

  function fei() public pure returns (uint8) {

      uint8 a = 3; // 0b00000011
      uint8 c = ~a; // 0b11111100
      return c; // 0
  }
  
  function yihuo() public pure returns (uint) {

      uint a = 3; // 0b0011
      uint b = 4; // 0b0100
    
      uint c = a ^ b; // 0b0111
      return c; // 252
  }
}

4.5.1.5. 位移

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn

contract Math {

  function leftShift() public pure returns (uint8) {

      uint8 a = 8; // 0b00001000
      uint8 c = a << 2; // 0b00100000
      return c; // 32
  }

  function rightShift() public pure returns (uint8) {

      uint8 a = 8; // 0b00001000
      uint8 c = a >> 2; // 0b00000010
      return c; // 2
  }

}



a << n 表示a的二进制位向左移动n位,在保证位数没有溢出的情况下等价于 a乘以2的n次方。
a >> n 表示a的二进制位向右移动n位,在保证位数没有溢出的情况下等价于 a除以2的n次方。
 

4.5.2. 字符串

string 字符串类型,字符串可以通过""或者''来定义字符串的值

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract StringTest {

    string name;
    
    function StringTest() public{
        name = "default";
    }
    function setName(string _name) public{
        name = _name;
    }
    function getName() public view returns(string){
        return name;
    }
}

4.5.2.1. 获取字符串长度

在 Solidity 中想获得字符串长度必须转成 bytes 类型然后使用 length 属性获得。bytes(string).length

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract StringTest {
    
    
    string public name = "http://www.netkiller.cn";
    

    function nameBytes() public constant returns (bytes) {
        
        return bytes(name);
    }
    
    function nameLength() public constant returns (uint) {
        
        return bytes(name).length;
    }

    function length(string _name) public pure returns (uint) {
        
        return bytes(_name).length;
    }
    
}
[提示] 提示

注意:汉字采用UTF8编码,一个汉字等于3个字节,当你使用 length("景峯") 测试时会返回长度 6。

4.5.3. 布尔(Booleans)

bool: 可能的取值为常量值true和false。支持的运算符:

! 逻辑非

&& 逻辑与

|| 逻辑或

== 等于

!= 不等于

bool a = true;
bool b = !a;

// a == b -> false
// a != b -> true
// a || b -> true
// a && b -> false

4.5.4. 字节类型

bytes names = "netkiller"
bytes9 _names = "netkiller";
bytes(name)[0] = 0xFF;

bytes memory _tmp = new bytes(3);
_tmp[0] = 0x4e;
_tmp[1] = 0x65;
_tmp[2] = 0x6f;
pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract BytesTest {
    
    bytes names = "netkiller";
    
    function get() public view returns (bytes) {
        
        return names;
    }
    function getBytes2() public pure returns (bytes2) {
        bytes9 _names = "netkiller";
        return bytes2(_names);
    }
    function bytesToString() public constant returns (string) {
        
        return string(names);
    }
   
    function copyBytes(bytes b) public pure returns (bytes) {
       
       bytes memory tmp = new bytes(b.length);
       
       for(uint i = 0; i < b.length; i++) {
           
           tmp[i] = b[i];
       }
       
       return tmp;
    }
    
    function bytesToString2() public pure returns (string) {
        bytes memory _tmp = new bytes(3);
        _tmp[0] = 0x4e;
        _tmp[1] = 0x65;
        _tmp[2] = 0x6f;
        return string(_tmp);
    }
   
}

.length可以动态修改字节数组的长度

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract BytesTest2 {
    
    // 初始化一个两个字节空间的字节数组
    bytes public array = new bytes(2);
    
    // 设置修改字节数组的长度
    function setLength(uint _len) public {
        array.length = _len;
    }
    
    // 返回字节数组的长度
    function getLength() constant public returns (uint) {
        return array.length;
    }
    
    // 往字节数组中添加字节
    function pushArray(byte _tmp) public{
        array.push(_tmp);
    }
    
}

4.5.5. 数组

//创建一个memory的数组
	uint[] memory a = new uint[](7);
	
	uint[] x = [uint(1), 3, 4];
	
    bytes memory b = new bytes(10);

二维数组

uint [2][3] T = [[1,2],[3,4],[5,6]];
pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract ArrayTest {
    
    uint [] array = [1,2,3,4,5];
    
    // 通过for循环计算数组内部的值的总和
    function sum() constant public returns (uint) {
        uint num = 0;
        for(uint i = 0; i < array.length; i++) {
            num = num + array[i];
        }
        return num;
    }
    
    function sumNumbers(uint[] _numbers) public pure returns (uint) {
        uint num = 0;
        for(uint i = 0; i < _numbers.length; i++) {
            num = num + _numbers[i];
        }
        return num;
    }
    
}

4.5.5.1. length

.length 属性是活动数组的尺寸

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract ArrayLength {
    
    uint [] array = [1,2,3,4,5];
    
    function getLength() public constant returns (uint) {
        
        return array.length;
    }
    
}

4.5.5.2. push() 方法

通过 push 可以向数组中添加数据

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract ArrayLength {
    
    uint [] array = [1,2,3,4,5];
    
    function pushArray() public {
        
        array.push(6);
    }
    
    function getLength() public constant returns (uint) {
        
        return array.length;
    }
    
}

4.5.6. 枚举类型

State 就是一个自定义的整型,当枚举数不够多时,它默认的类型为uint8,当枚举数足够多时,它会自动变成uint16,枚举下标定义从左至右从零开始。

New=0, Pending=1, Done=2, Deleted=3

访问枚举方式 State.New 实际等于数字 0

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract EnumTest {
    enum State { New, Pending, Done, Deleted }
    State state = State.New;

    function set(State _state) public {
        state = _state;
    }

    function get() constant public returns (State) {
        return state;
    }

}

枚举用来定义状态

pragma solidity ^0.4.0;

contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
}

4.5.7. 结构体

定义结构体

struct Voter {
        uint weight; // weight is accumulated by delegation
        bool voted;  // if true, that person already voted
        address delegate; // person delegated to
        uint vote;   // index of the voted proposal
    }

    // This is a type for a single proposal.
    struct Proposal {
        bytes32 name;   // short name (up to 32 bytes)
        uint voteCount; // number of accumulated votes
    }

演示例子

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Students {
    
    struct Person {
        string name;
        uint age;
        uint class;
        
    }

    Person person = Person("Neo",18,1);

    function getPerson() public view returns(string){
        return person.name;
    }
}

4.5.8. address

address public minter;

下面是一个获得账号余额的例子。

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract AddressTest{
    
    function getBalance(address _addr) public constant returns (uint){
        return _addr.balance;
    }

}

4.5.8.1. payable

4.5.8.2. .value()

4.5.8.3. .gas()

4.5.9. mapping

mapping 就是图数据结构,由 key 和 value 组成。

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract MappingExample {
    
    mapping(uint => string) map;

    function put(uint key, string value) public {
        map[key] = value;
    }
    
    function get(uint key) constant public returns (string) {
        return map[key];
    }
}

网友评论

登录后评论
0/500
评论
netkiller
+ 关注