1. pragma solidity ^0.4.10;
  2.  
  3. //枚举类型
  4. contract enumTest{
  5. enum ActionChoices{Left,Right,Straight,Still}
  6. // 0 1 2 3
  7. //input/output uint
  8. ActionChoices public _choice;
  9. ActionChoices defaultChoice = ActionChoices.Straight;
  10.  
  11. function setStraight(ActionChoices choice) public{
  12. _choice = choice;
  13. }
  14. function getDefaultChoice() constant public returns(uint){
  15. return uint(defaultChoice); //
  16. }
  17. function isLeft(ActionChoices choice) constant public returns(bool){
  18. if (choice == ActionChoices.Left){
  19. return true; //
  20. }
  21. return false; //1,2,3
  22. }
  23. }
  24. //构造函数
  25. contract StructTest{
  26. struct Student{
  27. string name;
  28. uint sex;
  29. uint age;
  30. //uint year =15;ParserError: Expected ';' but got '=':Cannot be assigned within the constructor,unless use constructor public{}
  31. }
  32.  
  33. Student public std1 = Student('lily',0,15);
  34. Student public std2 = Student({name:'lin',sex:1,age:17});
  35. /*
  36. 0: string: name lin
  37. 1: uint256: sex 1
  38. 2: uint256: age 17
  39. */
  40.  
  41. Student [] public students;
  42.  
  43. function assgin() public{
  44. students.push(std1);
  45. students.push(std2);
  46. std1.name ='eilinge';
  47. /*
  48. 0: string: name eilinge
  49. 1: uint256: sex 0
  50. 2: uint256: age 15
  51. */
  52. }
  53. }
  54.  
  55. //映射/字典
  56. contract mappingTest{
  57. mapping(uint => string) id_names;
  58.  
  59. /*
  60. mapping (_key => _values)
  61. 键的类型允许除映射外的所有类型,如数组、合约、枚举、结构体.值的类型无限制.
  62. 映射可以被视作一个哈希表,其中所有可能的键已被虚拟化的创建,被映射到一个默认值(二进制表示的零)
  63. 在映射表中,我们并不存储建的数据,仅仅储存它的keccak256哈希值,用来查找值时使用。
  64. 映射类型,仅能用来定义状态变量,或者是在内部函数中作为storage类型的引用
  65. */
  66. constructor() public{
  67. id_names[0x001] = 'jim';//构造函数内声明的全局变量,任意函数都可以进行调用
  68. id_names[0x002] = 'eilin';
  69. }
  70.  
  71. function getNamebyId(uint id) constant public returns(string){
  72. string name = id_names[id]; //局部变量 name,仅能在本函数内使用,其他函数无法进行调用
  73. return name;
  74. }
  75. }
  76.  
  77. //修改器
  78. pragma solidity ^0.4.10;
  79.  
  80. contract modifierTest{
  81. address owner=msg.sender;
  82. uint public v3;
  83.  
  84. modifier onlyowner(address own) {
  85. require(own == owner);
  86. _;
  87. }
  88. function setv3() onlyowner(msg.sender) {
  89. v3 = 10;
  90. }
  91. }

ethereum(以太坊)(七)--枚举/映射/构造函数/修改器的更多相关文章

  1. AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器

    本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...

  2. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...

  3. ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)

    pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...

  4. ethereum(以太坊)(实例)--"安全的远程购买"

    pragma solidity ^0.4.10; contract Safebuy{ uint public price; address public seller; address public ...

  5. ethereum(以太坊)(四)--值传递与引用传递

    contract Person { string public _name; function Person() { _name = "liyuechun"; } function ...

  6. ethereum(以太坊)(十一)--字节数组(二)

    pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...

  7. ethereum(以太坊)(十)--函数修饰符

    pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...

  8. ethereum(以太坊)(实例)--"简单的公开竞拍"

    说真的,刚开始接触这个竞拍案例--“简单的公开竞拍”,我就抱着简单的心态去查看这个实例,但是自我感觉并不简单.应该是我实力不到家的原因吧!!!233333...不过经过大半天的努力,自己理解完之后,觉 ...

  9. ethereum(以太坊)(十四)--Delete

    pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...

随机推荐

  1. 【转】android ViewPager,ViewFlipper,ViewFlow实现左右滑动

    转自:http://blog.csdn.net/zhouyuanjing/article/details/8290454 开篇 首页只是作为ViewPager,ViewFlipper,ViewFlow ...

  2. C++中的虚函数表

    (感谢http://blog.csdn.net/haoel/article/details/1948051/) C++中的虚函数的作用主要是实现了多态的机制. 多态,简而言之就是用父类型别的指针指向其 ...

  3. RNQOJ [stupid]愚蠢的矿工(树形依赖背包)

    题意 题目链接 Sol 树形依赖背包板子题 树形依赖背包大概就是说:对于一个点,只有选了它的父亲才能选自身 把dfs序建出来,倒过来考虑 设\(f[i][j]\)表示从第\(i\)个节点往后背包体积为 ...

  4. apache部署多域名,同个ip部署多个网站

    写个总结笔记,让以后的自己知道怎么部署. 首先apache的版本是2.4.7,然后系统是Ubuntu 14.04.1 LTS.(因为好像配置文件和目录有差异) 首先进到apache2目录下, 我们要探 ...

  5. textarea存起来的数据把空格也存起来

    textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...

  6. view 状态动画

    stateListAnimator 一.xml配置 方法 res/xml/animate_scale.xml <?xml version="1.0" encoding=&qu ...

  7. u-boot分析(二)----工作流程分析

    u-boot分析(二) 由于这两天家里有点事,所以耽误了点时间,没有按时更新,今天我首先要跟大家说说我对于u-boot分析的整体的思路,然后呢我以后的博客会按照这个内容更新,希望大家关注. 言归正传, ...

  8. mysql5.1安装图解教程

    mysql5.1安装图解教程 曾经为了要搭建模拟平台,从内地网站学习到台湾网站,其中必不可少的一项就是MySQL,那时候正好普遍用5.1,所以也很习惯用MySQL5.1版本! 下面就以MySQL5.1 ...

  9. Active Directory域服务备份

    此篇介绍如何通过Windows Server Backup工具备份Active Directory域服务 AD 域系统状态 在域控制器上,系统状态通常包括以下内容,但所包含的数据实际上取决于服务器上安 ...

  10. centos系统下安装Nginx

    参考链接 CentOS 7 用 yum 安装 Nginx Nginx负载均衡配置 下载并安装 #使用以下命令 sudo yum install -y nginx #sudo表示使用管理员权限运行命令 ...