-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_Structs.sol
More file actions
41 lines (34 loc) · 1.07 KB
/
19_Structs.sol
File metadata and controls
41 lines (34 loc) · 1.07 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
contract Structs {
struct Car {
string brand;
uint year;
address owner;
}
Car public car;
Car[] public cars;
function testCar() external {
// 创建一个结构体的几种方式
Car memory newCar = Car('bmw',1990, msg.sender);
Car memory newCar1 = Car({ brand: 'audi', year: 2000, owner: address(10)});
cars.push(newCar);
cars.push(newCar1);
Car storage car1 = cars[0];
car1.year = 2020;
delete car1.brand; // car1.brand => ''
// 那么会把 car 中的三个属性都变成默认值
delete cars[0];
}
uint[] public arr = [2,20,3];
function modifyValueByMemory() external view {
// 如果是 memory 修饰,那无法修改这个状态变量
uint[] memory a = arr;
a[0] = 10;
}
function modifyValueByStorage() external {
// 如果希望重新赋值去修改状态变量,则必须是 storage
uint[] storage b = arr;
b[0] = 20;
}
}