区块链学堂(24):Struct类型

我不是女神ヾ 2022-06-02 05:37 343阅读 0赞

Struct类型定义

例如定义一个struct类型的Person

  1. struct Person {
  2. string name;
  3. uint sexy; //0: 男性;1:女性;
  4. uint age;
  5. string mobile;
  6. }

建立一个Struct的Demo合约

  • Step 1: 撰写最基本的合约

    pragma solidity 0.4.10;
    contract DemoTypes9 {
    struct Person {

    1. string name;
    2. uint sexy; //0: 男性;1:女性;
    3. uint age;
    4. string mobile;

    }

  1. Person[] public PersonList;
  2. }

最初的合约只定义了struct person类型,以及一个数组Person[] PersonList

  • Step 2: 给构造函数中添加初始化代码

    1. function DemoTypes9() {
    2. /*数组类型添加元素方式1*/
    3. uint id = PersonList.length++;
    4. Person p = PersonList[id];
    5. p.name = "阿三";
    6. p.sexy = 0;
    7. p.age = 20;
    8. p.mobile = "13918802350";
    9. }
Step 1-2后,完整代码如下:
  1. pragma solidity 0.4.10;
  2. /*演示一下结构,如何和数组类型结合,一起使用;*/
  3. contract DemoTypes9 {
  4. struct Person {
  5. string name;
  6. uint sexy; //0: 男性;1:女性;
  7. uint age;
  8. string mobile;
  9. }
  10. Person[] public PersonList;
  11. function DemoTypes9() {
  12. /*数组类型添加元素方式1*/
  13. uint id = PersonList.length++;
  14. Person p = PersonList[id];
  15. p.name = "阿三";
  16. p.sexy = 0;
  17. p.age = 20;
  18. p.mobile = "13918802350";
  19. }
  20. }
以上代码在Browser-solidity中的执行结果如下图所示:

import_struct_01.png

  • Step 3: 添加一个AddPerson function, 使用之前的数组类型使用过的push方法

    function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {

    1. /*数组类型添加元素方式2*/
    2. Person memory tmp;
    3. tmp.name = _name;
    4. tmp.sexy = _sexy;
    5. tmp.age = _age;
    6. tmp.mobile = _mobile;
    7. PersonList.push(tmp);

    }

Browser-solidity的执行情况如下:

import_struct_02.png

  • Step4: 上面的AddPerson() 有点长,下面有一个更简单的方法 AddPerson2()

    function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {

    1. /*数组类型添加元素方式3*/
    2. uint id = PersonList.length++;
    3. PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});

    }

AddPerson2() 的方法简洁了很多
Browser-solidity的执行情况如下

import_struct_03.png

完整代码在下面:

  1. pragma solidity 0.4.10;
  2. /*演示一下结构,如何和数组类型结合,一起使用;*/
  3. contract DemoTypes9 {
  4. struct Person {
  5. string name;
  6. uint sexy; //0: 男性;1:女性;
  7. uint age;
  8. string mobile;
  9. }
  10. Person[] public PersonList;
  11. function DemoTypes9() {
  12. /*数组类型添加元素方式1*/
  13. uint id = PersonList.length++;
  14. Person p = PersonList[id];
  15. p.name = "阿三";
  16. p.sexy = 0;
  17. p.age = 20;
  18. p.mobile = "13918802350";
  19. }
  20. function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {
  21. /*数组类型添加元素方式2*/
  22. Person memory tmp;
  23. tmp.name = _name;
  24. tmp.sexy = _sexy;
  25. tmp.age = _age;
  26. tmp.mobile = _mobile;
  27. PersonList.push(tmp);
  28. }
  29. function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {
  30. /*数组类型添加元素方式3*/
  31. uint id = PersonList.length++;
  32. PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});
  33. }
  34. }

原文:http://www.ethchinese.com/?p=1047

QQ群:559649971 (区块链学堂粉丝群)
个人微信:steven_k_colin

stevenkcolin.jpg

获取最新区块链咨询,请关注《以太中文网》微信公众号:以太中文网

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读