- solidity的三种修饰符
view: 可以自由调用,因为它只是“查看”区块链的状态而不改变它
pure: 也可以自由调用,既不读取也不写入区块链
payable: 常常用于将代币发送给合约地址。
- 布尔运算
![](https://i-blog.csdnimg.cn/blog_migrate/d87778181c474b9976f0345697cf8113.png)
buer.png
- 整型特性与运算
![](https://i-blog.csdnimg.cn/blog_migrate/703ab51b741d55518791902afff8f936.png)
整型.png
- 位运算
![](https://i-blog.csdnimg.cn/blog_migrate/ee90a588d30d17e34ab826f25775b548.png)
位运算.png
注意:
- 在做相关的运算时,需要注意是否会溢出的问题;除零等异常问题
- 在solidity中,字面值会先计算再赋值,为了更好的保留精度
- 固定长度的字节数组(静态)
![](https://i-blog.csdnimg.cn/blog_migrate/365d2d30c568bbcf5e4b5b0f23b3c44b.png)
固定长度字节数组.png
- 固定长度字节数组的扩充和压缩
![](https://i-blog.csdnimg.cn/blog_migrate/a5f8b3353dbdf45d99c8ca68fcad0324.png)
固定长度字节数组扩充压缩.png
- 动态的字节数组
![](https://i-blog.csdnimg.cn/blog_migrate/7708369c86df7abfe8cf70418b586a60.png)
mmapping2.png
- 固定长度的字节数组与动态数组的转换
![](https://i-blog.csdnimg.cn/blog_migrate/1aa0eb0be3aa024b40699f5af36485fb.png)
固定长度字节数组与动态数组转换.png
-
修改string类型的数据
string.png
说明:
* 字符串可以使用单引号或者双引号
* 中文在string中使用utf8的编码方式存储,占用3个byte
* 特殊字符占1个byte
- 将动态bytes字节数组转换为string类型
![](https://i-blog.csdnimg.cn/blog_migrate/e51b1fa808eb4aeb150f136aa8e269e8.png)
动态bytes转换维string.png
- 将固定长度的字节数组转换为string类型
![](https://i-blog.csdnimg.cn/blog_migrate/c653b35b856f54812d6560e800029399.png)
固定长度字节数组转换string.png
- 定长数组
![](https://i-blog.csdnimg.cn/blog_migrate/90fa622944bac63a3643b27095af124d.png)
定长数组.png
- 动态数组
![](https://i-blog.csdnimg.cn/blog_migrate/0662f9cc6b3cf28e76e6f03f403c4369.png)
动态数组.png
- 二维数组
说明:- solidity的二维数组与其他语言不同,[2] [3]表示3行2列,而其他语言为2行3列。
- 在遍历数组的时候又会转变成普通的存储情况。比较奇怪。
- 二维动态数组与一维数组类似,可以改变其数组长度。
![](https://i-blog.csdnimg.cn/blog_migrate/a676f0accbf6d342563a84ce999f1f3f.png)
二维数组.png
- 数组字面值
![](https://i-blog.csdnimg.cn/blog_migrate/0255e7a0114fa7254e80442510542f40.png)
数组字面值.png
- 结构体的两种初始化方法
pragma solidity ^0.4.17;
contract StructTest{
struct student{
uint id;
string name;
mapping(uint=>string) map;
}
//默认为storage类型,只能通过storage类型操作结构体中的mapping类型数据
student storageStu;
//mapping类型可以不用在定义时候赋值,而其他类型不赋值则报错
function init() pure public returns(uint,string){
student memory stu = student(110,"yorick");
return (stu.id,stu.name);
}
function init2() pure public returns(uint,string){
student memory stu = student({name:"jerry",id:233});
return (stu.id,stu.name);
}
function init3() public returns(uint,string,string){
student memory stu = student({name:"tom",id:666});
//通过storage类型的变量操作结构体中的mapping
storageStu = stu;
storageStu.map[1] = "helloworld";
return (storageStu.id,storageStu.name,storageStu.map[1]);
}
//形参为结构体变量时,为memory类型,并且必须使用internal修饰函数。赋值时也需要指定为memory,否则报错
function test(student stu) internal {
//error
//student s = stu;
}
}
总结:
- 动态bytes的初始化
new bytes() 可以获取bytes的长度,内容。也可以修改长度和内容。 - string
不能够直接获取长度和内容,需要转换为bytes。注意其中中文占3个字节。 - 固定长度字节数组之间的转换
缩小截断右边内容,扩充在右侧补零。 - 固定长度字节数组和动态字节数组之间的转换
可以使用for循环动态添加元素 - 将动态bytes转换为string
可以直接使用string做强制类型转换。 - 将固定长度的字节数组转换为string
可以先将其转换为动态字节数组,再用string做强制类型转换。