來到第五天了!
今天就讓我們在合約裡儲存資料吧!
通過這個簡單的範例來學習怎麼宣告 State Variables 以及 constructor, getter, and setter 的應用。
Contract Source Code:
pragma solidity ^0.4.25;
contract StateVariables {
string name;
address owner;
constructor() public {
/* Initailize state variables. */
name = "unknown";
owner = msg.sender;
}
function setName(string _name) public returns (string) {
/* Only owner has the permission to modify its name. */
if (msg.sender == owner) {
name = _name;
} else {
revert("Permission denied.");
}
return name;
}
function getName() public view returns (string) {
return name;
}
}
本日影片:
https://youtu.be/0yFl7KbDLlU
Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF