ethereum(以太坊)(七)--枚举/映射/构造函数/修改器
2024-09-08 01:41:54
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;
}
}
最新文章
- git本地提交到远程仓库命令
- poj 3468
- hdu 2042 - 不容易系列之二
- python爬虫代码
- v.size() return size_t not int 返回无符号整型数
- 【二进制】FZU 2062 Suneast &; Yayamao
- Selenium webdriver 常见问题
- ArrayList 源码分析
- GIT入门笔记(7)- 修改文件并向版本库提交
- maven管理的jsp应用如何添加servlet、jsp相关依赖(org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.index_jsp)
- 设计一个卖不同种类车的4s店
- vue 及sass安装
- PHP中的反射模拟框架中控制器的调度
- android 图片内存管理
- react-router V4中的url参数
- HTML 引入Css样式
- (转)C++初始化与赋值
- Spring boot 配置文件 使用占位符号
- Navigation and Pathfinding
- WSDL语法