汕头百姓网,seo百度seo排名优化软件,可以做go分析的网站,百度搜索引擎的原理结构体
结构体是一种可以自行定义的数据类型#xff0c;其结构体内是复合的数据类型结构#xff0c;当单一数据类型不能满足时可以使用创建所需结构体。
结构体定义使用 struct#xff0c;例如以下示例#xff1a;
struct Human{uint age;string name;uint height;}以上…结构体
结构体是一种可以自行定义的数据类型其结构体内是复合的数据类型结构当单一数据类型不能满足时可以使用创建所需结构体。
结构体定义使用 struct例如以下示例
struct Human{uint age;string name;uint height;}以上代码中使用 struct 定义结构体在此不必使用 public 进行修饰因为 Human 这个结构体是一种数据类型的抽象使用 public 毫无意义。
其结构体内包含 uint 的 age 和 height 变量数据以及一个 string 类型的 name 数据这意味着在之后的使用中Human 这个类型的结构体变量可使用其中的数据例如 age 、name 和height。
接着可以创建对应的结构体 Human 类型的变量就像创建一个 uint 变量一样简单
Human public XiaoMing;
Human public XiaoHong;此时直接将创建的结构体 Human 当做一种数据类型即可。
使用时直接使用 点运算符 . 对其中的属性进行赋值或者取值即可
XiaoHong.age18;
XiaoHong.nameXiaoHong;
XiaoHong.height170;那么此时创建一个合约完整的结构体赋值操作如下
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.6;
contract StructDemo{struct Human{uint age;string name;uint height;}Human public XiaoMing;Human public XiaoHong;function testStruct()external{XiaoHong.age18;XiaoHong.nameXiaoHong;XiaoHong.height170;XiaoMing.age19;XiaoMing.nameXiaoMing;XiaoMing.height172;Human memory XiaoLvHuman(20,XiaoLv,168);}
}