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;
}
}