布尔 true false
return a && b; //与运算, 两者同时为真才为真
return a || b; //或运算,一者为真即为真
整形 integer
int 有符号 uint 无符号
int8~int256 默认int256后面的数字为占空间的大小
int8 8位整型, int256 256位整型, int = int256
uint8~uint256 默认uint256
uint8 8位无符号整型(正整数), 0~255
比较运算符 < =
位操作符 & | ~
三元运算: x < 10 ? 1 : 2;
整型溢出问题
uint8最大值是255,最小值是0。超出最大值或小于最小值则会溢出。
现版本已无需考虑溢出了。
// 使用枚举自定义一个类型 ActionChoices
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
// 定义一个ActionChoices类型的变量
ActionChoices choice;
//定义类型时可以直接赋值
ActionChoices defaultChoice = ActionChoices.GoStraight;
函数function
外部和内部函数 external internal
合约内部可以直接调用内部函数,不能调用外部函数。
定长字节数组
定长数组 bytes1~bytes32
字节不可修改,长度不可修改
可以像字符串一样使用
像整型一样比较和运算
像数组一样索引
bytes1 bytes2 ... bytes32
1字节等于8位二进制 2位十六进制
一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。中文标点占三个字节,英文标点占一个字节
byte = bytes1
bytes1 a = 0xb5; // [10110101]
contract testLiteral{
function test() public pure returns (int ) {
return -2e10;
}
function test2() public pure returns (uint ) {
uint a = 5/2 + 5/2;
return a;
}
function testSoBig() public pure returns (uint) {
return ((2**800 + 1) - 2**800);
}
// 数字常量表达式,一旦其中含有非常量表达式,它就会被转为一个非常量类型,不同类型的之间没法进行运算,
function test3() public {
uint128 a = 1; // a 不再是常量类型
// uint128 b = 2.5 + a + 0.5;
}
function testEn() public pure returns (bytes10) {
return "abc";
}
function testChinese() public pure returns (bytes20) {
return "饺子不错";
}
function testString() public pure returns (bytes10) {
// "\x61\x62"
return "Tiny\u718A";
}
function hexLiteralBytes() public pure returns (bytes2, bytes1, bytes1) {
bytes2 a = hex"aabb";
return (a, a[0], a[1]);
}
}
RE: Solidity开发指南