淘先锋技术网

首页 1 2 3 4 5 6 7

 

demo.sol(枚举类型):

pragma solidity ^0.4.0;

contract enumTest {
    enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }  // 定义枚举类型 uint8(0,1,2,3)
    ActionChoices _choice;     // 定义枚举类型的变量(取值范围:0,1,2,3)
    ActionChoices defaultChoice = ActionChoices.GoStraight;  // 用点的方式使用枚举元素

    function setGoStraight(ActionChoices choice) public {
        _choice = choice;   // 只能设置成0,1,2,3 超出范围会报错
    }

    function getChoice() constant public returns (ActionChoices)  {
        return _choice;
    }

    function getDefaultChoice() constant public returns (uint) {
        return uint(defaultChoice);  // 显示转换成整型
    }
    
    function isGoLeft(ActionChoices choice) constant public returns (bool){
        if (choice == ActionChoices.GoLeft){
            return true;
        }
        return false;
    }
    
}