ethereum(以太坊)(七)--枚举/映射/构造函数/修改器
- pragma solidity ^0.4.10;
- //枚举类型
- contract enumTest{
- enum ActionChoices{Left,Right,Straight,Still}
- // 0 1 2 3
- //input/output uint
- ActionChoices public _choice;
- ActionChoices defaultChoice = ActionChoices.Straight;
- function setStraight(ActionChoices choice) public{
- _choice = choice;
- }
- function getDefaultChoice() constant public returns(uint){
- return uint(defaultChoice); //
- }
- function isLeft(ActionChoices choice) constant public returns(bool){
- if (choice == ActionChoices.Left){
- return true; //
- }
- return false; //1,2,3
- }
- }
- //构造函数
- contract StructTest{
- struct Student{
- string name;
- uint sex;
- uint age;
- //uint year =15;ParserError: Expected ';' but got '=':Cannot be assigned within the constructor,unless use constructor public{}
- }
- Student public std1 = Student('lily',0,15);
- Student public std2 = Student({name:'lin',sex:1,age:17});
- /*
- 0: string: name lin
- 1: uint256: sex 1
- 2: uint256: age 17
- */
- Student [] public students;
- function assgin() public{
- students.push(std1);
- students.push(std2);
- std1.name ='eilinge';
- /*
- 0: string: name eilinge
- 1: uint256: sex 0
- 2: uint256: age 15
- */
- }
- }
- //映射/字典
- contract mappingTest{
- mapping(uint => string) id_names;
- /*
- mapping (_key => _values)
- 键的类型允许除映射外的所有类型,如数组、合约、枚举、结构体.值的类型无限制.
- 映射可以被视作一个哈希表,其中所有可能的键已被虚拟化的创建,被映射到一个默认值(二进制表示的零)
- 在映射表中,我们并不存储建的数据,仅仅储存它的keccak256哈希值,用来查找值时使用。
- 映射类型,仅能用来定义状态变量,或者是在内部函数中作为storage类型的引用
- */
- constructor() public{
- id_names[0x001] = 'jim';//构造函数内声明的全局变量,任意函数都可以进行调用
- id_names[0x002] = 'eilin';
- }
- function getNamebyId(uint id) constant public returns(string){
- string name = id_names[id]; //局部变量 name,仅能在本函数内使用,其他函数无法进行调用
- return name;
- }
- }
- //修改器
- pragma solidity ^0.4.10;
- contract modifierTest{
- address owner=msg.sender;
- uint public v3;
- modifier onlyowner(address own) {
- require(own == owner);
- _;
- }
- function setv3() onlyowner(msg.sender) {
- v3 = 10;
- }
- }
ethereum(以太坊)(七)--枚举/映射/构造函数/修改器的更多相关文章
- AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器
本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...
- ethereum(以太坊)(一)
从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...
- ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)
pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...
- ethereum(以太坊)(实例)--"安全的远程购买"
pragma solidity ^0.4.10; contract Safebuy{ uint public price; address public seller; address public ...
- ethereum(以太坊)(四)--值传递与引用传递
contract Person { string public _name; function Person() { _name = "liyuechun"; } function ...
- ethereum(以太坊)(十一)--字节数组(二)
pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...
- ethereum(以太坊)(十)--函数修饰符
pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...
- ethereum(以太坊)(实例)--"简单的公开竞拍"
说真的,刚开始接触这个竞拍案例--“简单的公开竞拍”,我就抱着简单的心态去查看这个实例,但是自我感觉并不简单.应该是我实力不到家的原因吧!!!233333...不过经过大半天的努力,自己理解完之后,觉 ...
- ethereum(以太坊)(十四)--Delete
pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...
随机推荐
- 【转】android ViewPager,ViewFlipper,ViewFlow实现左右滑动
转自:http://blog.csdn.net/zhouyuanjing/article/details/8290454 开篇 首页只是作为ViewPager,ViewFlipper,ViewFlow ...
- C++中的虚函数表
(感谢http://blog.csdn.net/haoel/article/details/1948051/) C++中的虚函数的作用主要是实现了多态的机制. 多态,简而言之就是用父类型别的指针指向其 ...
- RNQOJ [stupid]愚蠢的矿工(树形依赖背包)
题意 题目链接 Sol 树形依赖背包板子题 树形依赖背包大概就是说:对于一个点,只有选了它的父亲才能选自身 把dfs序建出来,倒过来考虑 设\(f[i][j]\)表示从第\(i\)个节点往后背包体积为 ...
- apache部署多域名,同个ip部署多个网站
写个总结笔记,让以后的自己知道怎么部署. 首先apache的版本是2.4.7,然后系统是Ubuntu 14.04.1 LTS.(因为好像配置文件和目录有差异) 首先进到apache2目录下, 我们要探 ...
- textarea存起来的数据把空格也存起来
textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...
- view 状态动画
stateListAnimator 一.xml配置 方法 res/xml/animate_scale.xml <?xml version="1.0" encoding=&qu ...
- u-boot分析(二)----工作流程分析
u-boot分析(二) 由于这两天家里有点事,所以耽误了点时间,没有按时更新,今天我首先要跟大家说说我对于u-boot分析的整体的思路,然后呢我以后的博客会按照这个内容更新,希望大家关注. 言归正传, ...
- mysql5.1安装图解教程
mysql5.1安装图解教程 曾经为了要搭建模拟平台,从内地网站学习到台湾网站,其中必不可少的一项就是MySQL,那时候正好普遍用5.1,所以也很习惯用MySQL5.1版本! 下面就以MySQL5.1 ...
- Active Directory域服务备份
此篇介绍如何通过Windows Server Backup工具备份Active Directory域服务 AD 域系统状态 在域控制器上,系统状态通常包括以下内容,但所包含的数据实际上取决于服务器上安 ...
- centos系统下安装Nginx
参考链接 CentOS 7 用 yum 安装 Nginx Nginx负载均衡配置 下载并安装 #使用以下命令 sudo yum install -y nginx #sudo表示使用管理员权限运行命令 ...