Solidity 练习:枚举
睡不醒的鲤鱼 2022-12-29 Web3 Solidity
# 一、题目说明
Solidity 支持枚举,它们对于模型选择和跟踪状态很有用。
# 二、任务列表
- 编写函数,将 status 设置为 Shipped。
# 三、解答代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract EnumExamples {
// Enum representing shipping status
enum Status {
// No shipping request
None,
Pending,
Shipped,
// Accepted by receiver
Completed,
// Rejected by receiver (damaged, wrong item, etc...)
Rejected,
// Canceled before shipped
Canceled
}
Status public status;
// Returns uint
// None - 0
// Pending - 1
// Shipped - 2
// Completed - 3
// Rejected - 4
// Canceled - 5
function get() external view returns (Status) {
return status;
}
// Update
function set(Status _status) external {
status = _status;
}
// Update to a specific enum
function cancel() external {
status = Status.Canceled;
}
// Reset enum to it's default value, 0
function reset() public {
delete status;
}
function ship() external {
status = Status.Shipped;
}
}
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
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