一、Int类型
int代表有符号的整型,也就是可以带负数
uint代表没有符号的整型,也就是从0开始的正整数
uint8 代表为2的8次方
uint256 代表为2的256次方
uint 默认为 uint256
int8 代表为-2的7次方到正2的7次方
int256 代表为-2的255次方,到正2的255次方;
int 默认为int256
二、数组类型comparisons: <=, <, ==, !=, >=, >
bit operator: &, |, ^,~
arithmetic operator: +, -, *, /, %, **, <<, >>
uint[] a;
int[] b;
length
和 push
。
pragma solidity ^0.4.0;
contract aaa{
uint[] a;
function aaa(){
a.push(111);
}
function add(uint n){
a.push(n);
}
function lenof() returns(uint len) {
return a.length;
}
function updata(uint index,uint value ){
if(index > a.length -1) throw;
a[index] = value;
}
function query(uint index) returns(uint value){
if(index >= a.length) throw;
return a[index];
}
function del(uint index){
if(index >= a.length) throw;
for(uint i = index;i < a.length-1;i++){
a[i] = a[i+1];
}
delete a[a.length-1];
a.length --;
}
}
上面代码很简单,定义了一个public的int数组a
, 以及一个方法add(uint a)等方法。
在browser-solidity中我们可以先add(111)
& add(222)
. 然后调用a[0] & a[1],来查看结果。还有其它操作
结果显示如下图所示:
三、string类型
string[] strArr
pragma solidity 0.4.10;
contract Demo {
string[] public strArr;
function Demo() {
strArr.push("init");
}
}
function Add(string str) {
strArr.push(str);
}
function Update(uint index, string str) {
if (index > strArr.length-1) throw;
strArr[index] = str;
}
大家注意一下,如果index>strArr.length-1, 此时应抛出异常
function ValueOf(uint index) returns (string str){
if (index > strArr.length-1) throw;
return strArr[index];
}
function DeleteAt(uint index) {
uint len = strArr.length;
if (index > len-1) throw;
for (uint i = index; i
四、Enum类型
enum ActionCode {Left,Right, Forward, Rollback}
ActionCode public b2;
pragma solidity 0.4.7;
contract Demo {
enum ActionCode {Left,Right, Forward}
ActionCode public b2;
}
function Demo() {
b2 = ActionCode.Left;
}
function f() returns (string str) {
if (b2 == ActionCode.Left) return "Left";
}
pragma solidity 0.4.10;
contract Demo {
enum ActionCode {Left,Right, Forward}
ActionCode public b2;
function Demo() {
b2 = ActionCode.Left;
}
function f() returns (string str) {
if (b2 == ActionCode.Left) return "Left";
}
}
function Left() {
b2 = ActionCode.Left;
}
function Right() {
b2 = ActionCode.Right;
}
function Forward() {
b2 = ActionCode.Forward;
}
if (b2 == ActionCode.Right) return "Right";
if (b2 == ActionCode.Forward) return "Forward";
pragma solidity 0.4.7;
contract Demo {
enum ActionCode {Left,Right, Forward}
ActionCode public b2;
function Demo() {
b2 = ActionCode.Left;
}
function Left() {
b2 = ActionCode.Left;
}
function Right() {
b2 = ActionCode.Right;
}
function Forward() {
b2 = ActionCode.Forward;
}
function f() returns (string str) {
if (b2 == ActionCode.Left) return "Left";
if (b2 == ActionCode.Right) return "Right";
if (b2 == ActionCode.Forward) return "Forward";
}
}
例如定义一个struct类型的Person
struct Person {
string name;
uint sexy; //0: 男性;1:女性;
uint age;
string mobile;
}
pragma solidity 0.4.10;
contract DemoTypes9 {
struct Person {
string name;
uint sexy; //0: 男性;1:女性;
uint age;
string mobile;
}
Person[] public PersonList;
}
最初的合约只定义了struct person类型,以及一个数组Person[] PersonList
function DemoTypes9() {
/*数组类型添加元素方式1*/
uint id = PersonList.length++;
Person p = PersonList[id];
p.name = "阿三";
p.sexy = 0;
p.age = 20;
p.mobile = "13918802350";
}
pragma solidity 0.4.10;
/*演示一下结构,如何和数组类型结合,一起使用;*/
contract DemoTypes9 {
struct Person {
string name;
uint sexy; //0: 男性;1:女性;
uint age;
string mobile;
}
Person[] public PersonList;
function DemoTypes9() {
/*数组类型添加元素方式1*/
uint id = PersonList.length++;
Person p = PersonList[id];
p.name = "阿三";
p.sexy = 0;
p.age = 20;
p.mobile = "13918802350";
}
}
function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {
/*数组类型添加元素方式2*/
Person memory tmp;
tmp.name = _name;
tmp.sexy = _sexy;
tmp.age = _age;
tmp.mobile = _mobile;
PersonList.push(tmp);
}
Browser-solidity的执行情况如下:
function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {
/*数组类型添加元素方式3*/
uint id = PersonList.length++;
PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});
}
AddPerson2() 的方法简洁了很多
Browser-solidity的执行情况如下
pragma solidity 0.4.10;
/*演示一下结构,如何和数组类型结合,一起使用;*/
contract DemoTypes9 {
struct Person {
string name;
uint sexy; //0: 男性;1:女性;
uint age;
string mobile;
}
Person[] public PersonList;
function DemoTypes9() {
/*数组类型添加元素方式1*/
uint id = PersonList.length++;
Person p = PersonList[id];
p.name = "阿三";
p.sexy = 0;
p.age = 20;
p.mobile = "13918802350";
}
function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {
/*数组类型添加元素方式2*/
Person memory tmp;
tmp.name = _name;
tmp.sexy = _sexy;
tmp.age = _age;
tmp.mobile = _mobile;
PersonList.push(tmp);
}
function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {
/*数组类型添加元素方式3*/
uint id = PersonList.length++;
PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});
}
}