-
Notifications
You must be signed in to change notification settings - Fork 0
/
20221017_结构体.sol
50 lines (40 loc) · 1.38 KB
/
20221017_结构体.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract structTest {
struct Car {
string model;
uint year;
address owner;
}
Car public car;
Car[] public cars;
mapping ( address => Car ) public carOwner;
function CarSet() public {
Car memory toyota = Car ("toyota", 1999, msg.sender);
Car memory bmw = Car ({model: "bmw", year: 1989, owner: msg.sender});
//注意此处 小括号中套大括号
Car memory fiat;
fiat.model = "fiat";
fiat.year = 1998;
fiat.owner = msg.sender;
cars.push (toyota);
cars.push (bmw);
cars.push (fiat);
//注意此处推入的是上面定义结构体的值,推入后就从局部变量变成了状态变量.
cars.push(Car("ferrai", 1898, msg.sender));
//可直接往数组中推入该结构体的数据内容.而不用定义该结构体的实例名称.
Car memory _car = cars[0];
_car.model;
_car.year;
_car.owner;
//在memory中的变量不可修改
Car storage _car1 = cars[0];
_car1.model = "tesla";
_car1.year = 2008;
//在storage里的变量,可以修改
delete _car.owner;
//所以toyota的string应该是结构体默认值model
delete cars[2];
//bmw的三个变量应该都是默认值
}
}