lfx区块链博客


lfx 微信:chen1048258,全球区块链职业教育布道师,通信和信息技术培养工程区块链高级授课专家。


微信
技术交流群

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

孔壹学院:国内区块链职业教育领先品牌

作者:黎跃春,区块链、高可用架构工程师 微信:liyc1215 QQ群:348924182 博客:http://liyuechun.org

自定义结构体

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

  • 「区块链部落」官方公众号

版权声明:博客中的文章版权归博主所有,未经授权,禁止转载,转载请注明出处,合作请联系:chen1048258(微信)

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦