24结构体
Car memory toyota=Car("Toyota",1990,msg.sender);
为什么用memory:在内存中创建这个实例
为什么后面 Car storage _car=cars[0]; 因为从区块链上读取这个实例
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract Structs{
struct Car{
string model;
uint year;
address owner;
}
// 创建
Car public car;
Car [] public cars;
mapping (address=>Car[]) public carsByOnwer;
// 为什么前面用memory:在内存中创建这个实例
// 为什么后面 Car storage _car=cars[0]; 因为从区块链上读取这个实例
function example() external {
//初始化
// 方法1:按照顺序赋值
Car memory toyota=Car("Toyota",1990,msg.sender);
// 方法2:标注好赋值给哪个字段
Car memory lambo=Car({
year:1980,
model:"Lamborghini",
owner:msg.sender
});
//先初始化再赋值
Car memory tesla;
tesla.model="Tesla";
tesla.year=2010;
tesla.owner=msg.sender;
//加入数组
cars.push(toyota);
cars.push(lambo);
cars.push(tesla);
cars.push(Car("Ferrari",2020,msg.sender));
Car storage _car=cars[0];
_car.year=1999;
delete _car.owner;
delete cars[1];
}
}