【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 6 | 資料結構 Data Structures

【Enum】

枚舉(enum)和 struct 非常類似,只是 enum 中的每個參數會自動從 0 開始依序初始化。

Enum具有以下的性質:

  • 內部已有整數型態的元素存在

  • 可以存進uint8的型態,如果數值數量介於0~255,若大於256個值則可宣告為uint16

enum ActionChoices { GoLeft, GoRight, GoStraight, SitsStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.Gostraight;

在 Solidity 中 enum 更常被用來進行模型選擇(model choice),或者追蹤狀態(track of state)。

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;
    }
}

struct 和 enum 的差別:

  • Struct 用於表示一個複雜變數

  • Enum 用於表示一個單一變數的不同狀態或值

Previous【Structs】Next【Practice】

Last updated 3 years ago

Was this helpful?