enum ActionChoices { GoLeft, GoRight, GoStraight, SitsStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.Gostraight;
pragma solidity ^0.8.11;
contract MyEnums {
enum Rarity {
original, // 0
rare, // 1
super_rare // 2
}
Rarity public rarity;
constructor() {
rarity = Rarity.rare;
}
// 我們可以直接將一個型態為該 enum 的變數賦值為某特定屬性
function makeSuperRare() public {
rarity = Rarity.super_rare;
}
// 我們可以藉由 enum 初始化的 INDEX 來進行賦值
function set(Rarity _rarity) public {
rarity = _rarity;
}
// 我們可以藉由 delete 來重置這個 enum 成他的初始值 0
function reset() public {
delete rarity;
}
}