Solidity的自定义结构体深入详解
一.结构体定义
  结构体,Solidity中的自定义类型。我们可以使用Solidity的关键字struct来进行自定义。结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型。数组,映射,结构体也支持自定义的结构体。我们来看一个自定义结构体的定义:
pragma solidity ^0.4.4;
pragma solidity ^0.4.0;
contract SimpleStruct{
  //学生
  struct Student{
    string name;
    int num;
  }
  //班级
  struct Class{
    string clsName;
    //学生的列表
    Student[] students;
    mapping(string=>Student)index;
  }
}
在上面的代码中,我们定义了一个简单的结构体Student,它包含一些基本的数据类型。另外我们还定义了一个稍微复杂一点的结构体Class,它包含了其它结构体Student,以及数组,映射等类型。
数组类型的students和映射类型的index的声明中还使用了结构体。
结构体定义的限制
  我们不能在结构中定义一个自己作为类型,这样限制原因是,自定义类型的大小不允许是无限的。我们来看看下述的代码:
pragma solidity ^0.4.0;
contract NoMemberOfOwn{
  struct A{
    //定义包含自己的会报错
    //Error: Recursive struct definition.
    //A a;
    mapping(int=>A) mappingMemberOfOwn;
    A[] arrayMemberOfOwn;
  }
}
在上面的代码中,我们尝试在A类型中定义一个A a;,将会报错Error: Recursive struct definition.。虽然如此,但我们仍然能在类型内用数组,映射来引用当前定义的类型,如变量mappingMemberOfOwn,arrayMemberOfOwn所示。
二.初始化
  下面我们来说说结构体的初始化。
1.直接初始化
  如果我们声明的自定义类型为A,我们可以使用A(变量1,变量2, ...)的方式来完成初始化。来看下面的代码:
pragma solidity ^0.4.0;
contract StructInitial{
  struct A{
    string name;
    mapping(address=>A) map;
    int age;
    string[] cources;
  }
  function init() returns (string, int, string){
    string[] memory cources = new string[](1);
    cources[0] = "Chemistry";
    //按顺序填值,初始化时,可以跳过映射类型
    A memory a = A("Jack", 23, cources);
    return (a.name, a.age, cources[0]);
  }
}
上面的代码中,我们按定义依次填入值,即可完成了初始化。需要注意的是,参数要与定义的数量匹配。当你填的参数与预计初始化的参数不一致时,会提示Error: Wrong argument count for function call: 2 arguments given but expected 3. Members that have to be skipped in memory: map。另外,在初始化时,需要忽略映射类型1,后面有具体说明。
2.命名初始化
还可以使用类似JavaScript的命名参数初始化的方式,通过传入参数名和对应值的对象。这样做的好处在于可以不按定义的顺序传入值。我们来看看下面的例子:
pragma solidity ^0.4.0;
contract StructNamedInitial{
  struct Student{
    string name;
    mapping(address=>Student) map;
    int age;
    string[] cources;
  }
  function init() returns (string, int, string){
    string[] memory crs = new string[](1);
    crs[0] = "Chemistry";
    //按命名参数的方式进行初始化
    Student memory s = Student({
        age : 10,
        name : "Jack",
        cources: crs
      });
    return (s.name, s.age, s.cources[0]);
  }
}
上面的例子中,通过在参数对象中,指定键为对应的参数名,值为你想要初化的值,我们即完成了初始化。同样需要注意的是,参数要与定义的个数一致,否则会报类似这样的错误Error: Wrong argument count for function call: 2 arguments given but expected 3. Members that have to be skipped in memory: map。另外,在初始化时,需要忽略映射类型[ignore],后面有具体说明。
3.结构体中映射的初始化
  由于映射是一种特殊的数据结构。
Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is not actually stored in a mapping, only its keccak256 hash used to look up the value. Because of this, mappings do not have a length or a concept of a key or value being “set”.
  可以你可能只能在storage变量中使用它。
pragma solidity ^0.4.0;
contract StructMappingInitial{
  struct A{
    string name;
    mapping(address=>A) map;
    int age;
    string[] cources;
  }
  //分配映射的空间
  A storageVar;
  function init() returns (string, int, string){
    string[] memory cources = new string[](1);
    cources[0] = "Chemistry";
    A memory a = A("Jack", 23, cources);
    storageVar = a;
    storageVar.map[msg.sender] = a;
    return (a.name, a.age, cources[0]);
  }
}
上面的例子中,我们定义的了一个storage的状态变量storageVar,完成了映射类型的存储空间分配。然后我们就能对映射类型赋值了。
如果你尝试对memory的映射类型赋值,会报错Error: Member "map" is not available in struct StructMappingInitial.A memory outside of storage.。
三.结构体的可见性
  关于可见性,当前只支持internal的,后续不排除放开这个限制。详见开发者christen的讨论:
Since all variables are pre-initialised in Solidity, so are return values of struct type (with their members being initialised recursively). This means if you use
function f() internal returns (Record r) { ... } you could also just assign the members of r individually.
The struct itself resides in memory and the function returns a reference to this point in memory. This means that in the case of "return records[recordID]", the storage-struct is first copied to memory and then the function returns a reference to this place in memory. If you would like to return the storage reference itself, you have to use "function ... returns (Record storage) { ... }".
继承中使用
  结构体由于是不对外可见的,所以你只可以在当前合约,或合约的子类中使用。包含自定义结构体的函数均需要声明为internal的。
pragma solidity ^0.4.0;
contract A{
  struct S{
    string para1;
    int para2;
  }
  function f(S s) internal{
      //...
  }
  function f1(){
    //当前类中使用结构体
    S memory s = S("Test", 10);
    f(s);
  }
}
contract B is A{
  function g(){
      //字类中使用结构体
      S memory s = S("Test", 10);
      //调用父类方法
      f(s);
  }
}
在上面的代码中,我们声明了f(S s),由于它包含了struct的S,所以不对外可见,需要标记为internal。你可以在当前类中使用它,如f1()所示,你还可以在子类中使用函数和结构体,如B合约的g()方法所示。
四.跨合约的临时解决方案
  结构体,由于是动态内容。当前不支持在多个合约间互用,目前一种临时的方案如下:
pragma solidity ^0.4.0;
contract StructAcrossInitial{
  struct A{
    string para1;
    int para2;
  }
  function call(B b){
    A memory a = A("Test", 10);
    b.g(a.para1, a.para2);
  }
}
contract B{
  function g(string para1, int para2){
    //你要实现的内容
  }
}
在上面的例子中,我们手动将要返回的结构体拆解为基本类型进行了返回。
文章来源:http://me.tryblockchain.org/solidity-struct.html
Solidity的自定义结构体深入详解的更多相关文章
- go语言之行--结构体(struct)详解、链表
		
一.struct简介 go语言中没有像类的概念,但是可以通过结构体struct实现oop(面向对象编程).struct的成员(也叫属性或字段)可以是任何类型,如普通类型.复合类型.函数.map.int ...
 - inode结构体成员详解
		
概述:inode译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间.档名.使用者及群组等.inode分为内存中的inode和文件系统中的inode,为了避免混淆,我们称前者为VFS ino ...
 - IPv4地址结构体sockaddr_in详解
		
sockaddr_in结构体定义 struct sockaddr_in { sa_family_t sin_family; //地址族(Address Family) uint16_t sin_por ...
 - 结构体指针,C语言结构体指针详解
		
结构体指针,可细分为指向结构体变量的指针和指向结构体数组的指针. 指向结构体变量的指针 前面我们通过“结构体变量名.成员名”的方式引用结构体变量中的成员,除了这种方法之外还可以使用指针. 前面讲过,& ...
 - struct stat结构体的详解和用法
		
[cpp] view plaincopy //! 需要包含de头文件 #include <sys/types.h> #include <sys/stat.h> S_ISLNK( ...
 - Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解
		
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
 - typedef和自定义结构体类型
		
在自定义结构体类型时会用到typedef关键字.大家都知道typedef是取别名的意思,在C语言中跟它容易混淆的有const,#define等,其区别不在本篇文章讨论之列. /*定义单链表结点类型*/ ...
 - qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)
		
参考博文:QVariant与自定义数据类型转换的方法. 这里摘取其关键内容: 1.将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别. 2.在插入对象的时候,声明QVa ...
 - iOS自定义结构体
		
一.提要 通过以官方的CGSize为例,自定义Objective-C中的结构体,并使用. 二.CGSize 1.系统定义的CGSize结构体 struct CGSize { CGFloat width ...
 
随机推荐
- 修改input标签type=file类型的文字
			
<form name="form" id="form" method="post" enctype="multipart/f ...
 - POJ1742--Coins(动态规划)
			
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony ...
 - 第81讲:Scala中List的构造和类型约束逆变、协变、下界详解
			
今天来学习一下scala中List的构造和类型约束等内容. 让我们来看一下代码 package scala.learn /** * @author zhang */abstract class Big ...
 - windows 10 下配置安装node.js
			
环境配置 node.js windows10 25.5k 次阅读 · 读完需要 6 分钟 5 在去年就自己配置安装过node.js,但是使用npm安装模块时安装成功后调用require('mo ...
 - hdu 3030
			
这道题主要就是问你,长度为n的序列,有多少种上升的子序列 当前点的情况种数等于前面所有小于它的点的种数相加 + 1 1就是只有这一个点的时候的序列 那就是要多次查询前面比它小的点的种数的和 那么就是区 ...
 - centeros6.8 下安装mysql教程
			
1.1 安装Mysql 1.1.1 检查 l 检查是否已安装mysql的相关包 [root@localhost ~]# rpm -qa|grep -i mysql 一般情况下,centeros系统中会 ...
 - [Chrome_Error] (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING 与 nginx 502 bad gateway
			
Chrome 浏览器出现这个错误,还出现 nginx 502 bad gateway . 查看 nginx 的 error.log : 2015/12/18 14:34:44 [error] 1448 ...
 - 在linux上搭建nexus私服(CentOS7)
			
1.下载nexus安装包,下载地址 https://www.sonatype.com/download-oss-sonatype?hsCtaTracking=920dd7b5-7ef3-47fe-96 ...
 - 让你的照片更鲜艳------hsv拉伸
			
如果你的照片看上去灰蒙蒙的,缺少生机,那么hsv拉伸也许可以帮你的忙.hsv拉伸是一种可以提高图像鲜艳程度的图像增强方法,它能够让图像的颜色更加鲜活.艳丽,而且它的处理结果看上去很自然,比如源图中较暗 ...
 - Angular使用总结  ---  搜索场景中使用rxjs的操作符
			
在有input输入框的搜索/过滤业务中,总会考虑如何减少发起请求频率,尽量使每次的请求都是有效的.节流和防抖是比较常见的做法,这类函数的实现方式也不难,不过终归还是需要自己封装.rxjs提供了各种操作 ...