Solitidy变量类型

整形 int / uint:有符号和无符号整形数字,长度为256个字节,int代表int256,uint代表uint256;
也可声明8的倍数的整形,如int8,uint8到uint256;
为什么会使用低位的整形呢?因为智能合约对每一个字节的存储都是需要gas的,所以选择一个合适的长度会有效减省gas费用。
操作: 比较 <=, <, ==, !=, >=, > 返回(bool)
位运算 &,|,^(按位异或),~(按位取反)
算术运算 +,-,*,/,%,**(乘方),<<(左移),>>(右移)
左移运算符 x << y 和 x * 2**y 是相等的,x >> y 和 x / 2**y 是相等的。
地址类型 Address :20个字节长度,地址是所有智能合约的基础。
操作:<=,<,==,!=,>=,>
属性:Balance
方法:send,call,callcode,delegatecall
数组 变长字节数组,数组长度由自己去定义;
例: uint[10] a = {1,2,3};
二维数组 uint[2][3] uu;
固定字节数组 bytes1,bytes2,bytes3...bytes32,其中byte是bytes1的别名。
操作:索引访问:如果x是bytesI类型的,那么x[k]返回第k个字节,0<=k 例:bytes1 public b1 = 255;
bytes2 public b2 = "ab";
字符串 string 在solitidy中只是基本的存储字符串数据,定义好后不可改变,除了读之外不能对他进行别的任何操作,比如截取、合并等等;可声明数组。
例:string public str = "这是字符串";
枚举 enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
函数类型 函数类型是一种表示函数的类型。可以将一个函数赋值给另一个函数类型的变量,也可以将一个函数作为参数进行传递,还能在函数调用中返回函数类型变量。
结构体 struct Student{
address add;
【Solitidy变量类型】uint age;
}
映射 是一种关联,类似于Map集合的键值对。
mapping (uint => Student) public student;
例:
pragma solidity ^0.4.18;
contract MappingDemo{
//结构体
struct Account{
address add;
uint balance;
}
//映射变量
uint public mapNum;
//映射
mapping(uint => Account) public accounts;
//事件
event e(string _str,address _add,uint balance);
//新建账户
function newAccount(address _add,uint balance) public returns(uint){
++mapNum;
accounts[mapNum] = Account(_add,balance);
emit e("newAccount",_add,balance);
}
//修改余额
function setAccount(uint _u,uint _balance) public{
Account storage a = accounts[_u];
a.balance = _balance;
emit e("setAccount",a.add,a.balance);
}
}

    推荐阅读