ethereum(以太坊)(七)--枚举/映射/构造函数/修改器
生活随笔
收集整理的這篇文章主要介紹了
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); //2
}function isLeft(ActionChoices choice) constant public returns(bool){if (choice == ActionChoices.Left){return true; //0
}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 lin1: uint256: sex 12: uint256: age 17*/Student [] public students;function assgin() public{students.push(std1);students.push(std2);std1.name ='eilinge';/*0: string: name eilinge1: uint256: sex 02: 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;}
}
?
轉載于:https://www.cnblogs.com/eilinge/p/9959372.html
總結
以上是生活随笔為你收集整理的ethereum(以太坊)(七)--枚举/映射/构造函数/修改器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式版本控制工具Git
- 下一篇: vue源码之响应式数据