demo.sol(字典类型(mapping,映射)):
pragma solidity ^0.4.20;
contract test {
// key => value (key不能重复)
mapping(uint => string) map; // 字典(mapping)类型 (映射类型)
constructor() public {
map[0x001] = "lily";
map[0x002] = "Jim";
map[0x002] = "Lily"; // 会覆盖之前的值
}
function getNameById(uint id) constant public returns (string) {
string storage name = map[id];
return name;
}
}