// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Enum {
enum Status {
None, // 0
Pending, // 1
Shipped, // 2
Completed, // 3
Rejected,
Canceled
}
Status public status;
Status constant defaultStatus = Status.None;
// 枚举可以和其他数据类型自由组合
struct Order {
address buyer;
Status status;
}
Order[] public orders;
function get() view external returns (Status) {
return status;
}
function getDefault() pure external returns (uint) {
return uint(defaultStatus);
}
// input: 1
function set(Status _status) external {
require(_status == Status.Pending);
status = _status;
}
function ship() external {
status = Status.Shipped;
}
// 将枚举恢复成默认值,默认值即第一个值None
function reset() external {
delete status;
}
}