【Solidity】Struct的使用方法
Struct和enum很类似,enum只能是数字,但是struct就比较丰富了,能够定义variable的种类
代码范例
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract DemoStruct{ struct car{ string brand; uint year; } car[] public cars; function modify()public{ car memory car1 = car("Toyota",2008); //声明struct, 方法1 car memory car2 = car({year:2018,brand:"Honda"});//声明struct, 方法2 car memory car3;//声明struct, 方法3 car3.brand = "Nissan"; car3.year = 2011; cars.push(car1); cars.push(car2); cars.push(car3); delete cars[0];//把第一个car给删除了,所以car0的值全部变成默认值 } }
Facebook评论