1 Star 0 Fork 11

Rayest / solidity-expert

forked from duke.du / solidity-expert 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
15_枚举Enum.md 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
duke.du 提交于 2022-09-02 08:59 . optimize, add roadmap for contract audit

第15节:枚举Enum

枚举可以避免魔数,让程序更加易读,更好的进行状态管理,默认第一个值是:0

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Enum {
    // Enum representing shipping status
    enum Status {
        Pending,
        Shipped,
        Accepted,
        Rejected,
        Canceled
    }

    // Default value is the first element listed in
    // definition of the type, in this case "Pending"
    Status public status;

    // Returns uint
    // Pending  - 0
    // Shipped  - 1
    // Accepted - 2
    // Rejected - 3
    // Canceled - 4
    function get() public view returns (Status) {
        return status;
    }

    // Update status by passing uint into input
    function set(Status _status) public {
        status = _status;
    }

    // You can update to a specific enum like this
    function cancel() public {
        status = Status.Canceled;
    }

    // delete resets the enum to its first value, 0
    function reset() public {
        delete status;
    }
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/rayest1108/solidity-expert.git
git@gitee.com:rayest1108/solidity-expert.git
rayest1108
solidity-expert
solidity-expert
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891