0
点赞
收藏
分享

微信扫一扫

『0018』 - Solidity Types - Solidity 结构体(Structs)

自定义结构体

pragma solidity ^0.4.4;

contract Students {

    struct Person {
        uint age;
        uint stuID;
        string name;
    }

}

Person就是我们自定义的一个新的结构体类型,结构体里面可以存放任意类型的值。

初始化一个结构体

初始化一个storage类型的状态变量。

  • 方法一

pragma solidity ^0.4.4;

contract Students {

    struct Person {
        uint age;
        uint stuID;
        string name;
    }

    Person _person = Person(18,101,"liyuechun");

}

  • 方法二

pragma solidity ^0.4.4;

contract Students {

    struct Person {
        uint age;
        uint stuID;
        string name;
    }

    Person _person = Person({age:18,stuID:101,name:"liyuechun"});

}

初始化一个memory类型的变量。

pragma solidity ^0.4.4;

contract Students {

    struct Person {
        uint age;
        uint stuID;
        string name;
    }

    function personInit() {

        Person memory person = Person({age:18,stuID:101,name:"liyuechun"});
    }
}

技术交流

  • 区块链技术交流QQ群:348924182
  • 「区块链部落」官方公众号


举报

相关推荐

0 条评论