Solidity 智慧合約開發語言·資料型別
本文節選自電子書《Netkiller Blockchain 手札》
Netkiller Blockchain 手札
本文作者最近在找工作,有意向致電 13113668890
Mr. Neo Chan, 陳景峰(BG7NYT)
中國廣東省深圳市龍華新區民治街道溪山美地
518131
+86 13113668890
<[email protected]>
文件始創於2018-02-10
版權 © 2018 Netkiller(Neo Chan). All rights reserved.
版權宣告
轉載請與作者聯絡,轉載時請務必標明文章原始出處和作者資訊及本宣告。
http://www.netkiller.cnhttp://netkiller.github.iohttp://netkiller.sourceforge.net |
http://www.netkiller.cn |
http://netkiller.github.io |
http://netkiller.sourceforge.net |
微信訂閱號 netkiller-ebook (微信掃描二維碼)QQ:13721218 請註明“讀者”QQ群:128659835 請註明“讀者” |
微信訂閱號 netkiller-ebook (微信掃描二維碼) |
QQ:13721218 請註明“讀者” |
QQ群:128659835 請註明“讀者” |
||||
---|---|---|---|---|---|---|---|---|---|---|---|
http://www.netkiller.cn | |||||||||||
http://netkiller.github.io | |||||||||||
http://netkiller.sourceforge.net | |||||||||||
微信訂閱號 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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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 <[email protected]>
//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];
}
}