区块链学堂(23):Enum数据类型
2018-01-05 09:22
246 查看
Enums类型的官方定义
Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversions check the value ranges at runtime and a failure causes an exception.
Enums needs at least one member.here
Step 1: 首先定义enum类型
Step 2: 设置一个构造函数,并给变量b2赋予一个初始值
Step 3:设置一个判断函数,输出结果
Step1-3的完整代码如下:
Step 1-3,实现了enum(枚举)类型的功能,在构造函数中设置变量b2的初始值,然后根据b2的value来进行判断。
Step 4: 在Step1-3的基础上 添加几个function。Left()/Right()/Forward()
Step 5: 修改f(), 增加两个输出
完整代码如下:
以上完整的代码,实现了left(),right() & forward() 方法
原文:http://www.ethchinese.com/?p=1044
QQ群:559649971 (区块链学堂粉丝群)
个人微信:steven_k_colin
获取最新区块链咨询,请关注《以太中文网》微信公众号:以太中文网
Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversions check the value ranges at runtime and a failure causes an exception.
Enums needs at least one member.here
Enum类型定义
enum ActionCode {Left,Right, Forward, Rollback} ActionCode public b2;
Enum的Demo代码
Step 1: 首先定义enum类型pragma solidity 0.4.7; contract Demo { enum ActionCode {Left,Right, Forward} ActionCode public b2; }
Step 2: 设置一个构造函数,并给变量b2赋予一个初始值
function Demo() { b2 = ActionCode.Left; }
Step 3:设置一个判断函数,输出结果
function f() returns (string str) { if (b2 == ActionCode.Left) return "Left"; }
Step1-3的完整代码如下:
pragma solidity 0.4.10; contract Demo { enum ActionCode {Left,Right, Forward} ActionCode public b2; function Demo() { b2 = ActionCode.Left; } function f() returns (string str) { if (b2 == ActionCode.Left) return "Left"; } }
Step 1-3,实现了enum(枚举)类型的功能,在构造函数中设置变量b2的初始值,然后根据b2的value来进行判断。
Step 4: 在Step1-3的基础上 添加几个function。Left()/Right()/Forward()
function Left() { b2 = ActionCode.Left; } function Right() { b2 = ActionCode.Right; } function Forward() { b2 = ActionCode.Forward; }
Step 5: 修改f(), 增加两个输出
if (b2 == ActionCode.Right) return "Right"; if (b2 == ActionCode.Forward) return "Forward";
完整代码如下:
pragma solidity 0.4.7; contract Demo { enum ActionCode {Left,Right, Forward} ActionCode public b2; function Demo() { b2 = ActionCode.Left; } function Left() { b2 = ActionCode.Left; } function Right() { b2 = ActionCode.Right; } function Forward() { b2 = ActionCode.Forward; } function f() returns (string str) { if (b2 == ActionCode.Left) return "Left"; if (b2 == ActionCode.Right) return "Right"; if (b2 == ActionCode.Forward) return "Forward"; } }
以上完整的代码,实现了left(),right() & forward() 方法
在Browser-solidity上的演示效果如下:
原文:http://www.ethchinese.com/?p=1044
QQ群:559649971 (区块链学堂粉丝群)
个人微信:steven_k_colin
获取最新区块链咨询,请关注《以太中文网》微信公众号:以太中文网
相关文章推荐
- 《区块链学堂》第四课:以太坊高级数据类型和代币合约(附课程视频)
- C#学习日记11---数据类型 之 枚举(enum)类型
- C语言enum(枚举)数据类型
- 区块链学堂(20):Int类型
- 区块链学堂(21):数组类型
- 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组
- Java基础知识强化23:Java中数据类型转换(面试题)
- 把对象生成为Parcel类型,以及如何利用Parcel来传递Enum类型数据
- 区块链学堂(22):String 数组类型
- [C#]DataGridView中使用数据绑定Enum类型
- C# 权举 enum 与数据类型之间的转换
- 关于struct,union,enum三大复合数据类型
- ORM框架中用枚举类型enum来接收数据库中的tinyint字段
- C数据类型(枚举 enum )和switch语句
- 使用enum枚举数据类型实现多线程中的单例模式
- 用enum类型数据解决switch case选择字符串的问题
- 学点 C 语言(18): 数据类型 - 枚举类型(enum)
- 区块链学堂(24):Struct类型
- 用enum类型数据解决switch case选择字符串的问题
- PHP学习笔记(23)PHP的数据类型与常量使用(24)PHP的运算符与表达式