當我們需要一個學生的類別時,原生的 type 並不能滿足我們,這時候就到了 struct 出場啦!他能幫助我們自己定義所需要的 type 。
本日合約:
pragma solidity ^0.4.25;
contract Class {
struct Student {
string name;
uint score;
bool active;
}
mapping(uint => Student) students;
modifier ActiveStudent(uint id) {
require(students[id].active, "Student is inactive");
_;
}
function register(uint id, string name) public {
students[id] = Student({name: name, score: 0, active: true});
}
function modifyScore(uint id, uint score) public ActiveStudent(id) {
students[id].score = score;
}
function getStudent(uint id) public ActiveStudent(id) view returns (string, uint) {
return (students[id].name, students[id].score);
}
}
本日影片:
https://youtu.be/62I86dgVc2g
Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF