智能合約開發實戰(12) - struct

2022/04/02閱讀時間約 8 分鐘
這篇我們來講一下還蠻重要也蠻常用到的一個自定義型別,叫:struct
struct是來自於C/C++的結構,目的是把不同資料型態的參數串在一起,成為一個自定義的資料型態,我們也稱它是一個自定義的結構體。
為什麼需要struct呢?因為有struct後,我們可以定義一個結構體,把一堆資料型態串在一起,比如說定義一個學生student:
struct student {

  uint studentID;//學號

  string Name;//姓名

  string class;//班級
  uint score;//成績

}
學生就會有學號、姓名、班級、成績…等,這樣我們就可以對這個學生做資料的存取,而且也可以創造很多個這樣的學生,比如說:
//宣告一個學生的Array
student[] public studentList;
加了一個之前有教過的Array,這樣學生就會是一個群體了。
接下來就寫一個function來創建一個學生:
function create(uint _studentID, string memory _name, string memory _class, uint _score) public {
  //第一種寫法
  //Student memory student = Student(_studentID, _name, _class, _score);
  //第二種寫法(必須像Json)
  // Student memory student =Student({
  //     studentID: _studentID,
  //     name: _name,
  //     class: _class,
  //     score: _score
  // });
  //第三種寫法
  Student memory student;
  student.studentID = _studentID;
  student.name = _name;
  student.class = _class;
  student.score = _score;
  studentList.push(student);//寫入學生群組
}
上面的有三種寫法都是可以用來Create學生,最後會加入學生群組studentList,然後我們來寫一個get的function,取出來看一下。
function get(uint _index) public view returns (Student memory) {
  return studentList[_index];
}
寫到這裡我們可以發佈上去看一下:
看起來正常,也能從get取出資料,接下來繼續加,有get當然要寫個set:
//設定姓名
function setName(uint _index, string memory _name) public {
  //驗證
  require(_index < studentList.length, "index must be less than the group maximum");
  Student storage student = studentList[_index];
  student.name = _name;
}
//設定成績
function setScore(uint _index, uint _score) public {
  //驗證
  require(_index < studentList.length, "index must be less than the group maximum");
  Student storage student = studentList[_index];
  student.score = _score;
}
上面寫的是可以重新設定姓名和成績,發佈看一下效果:
修改姓名和成績:
最後顯示整個學生群組(studentList):
//顯示整個student group
function showStudentGroup() public view returns (Student[] memory) {
  return studentList;
}
完整程式碼:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract Struct {
  struct Student {
    uint studentID;//學號
    string name;//姓名
    string class;//班級
    uint score;//成績
  }
  
  //學生Array群組
  Student[] public studentList;
  function create(uint _studentID, string memory _name, string memory _class, uint _score) public {
    //第一種寫法
    //Student memory student = Student(_studentID, _name, _class, _score);
    //第二種寫法(必須像Json)
    // Student memory student =Student({
    //     studentID: _studentID,
    //     name: _name,
    //     class: _class,
    //     score: _score
    // });
    //第三種寫法
    Student memory student;
    student.studentID = _studentID;
    student.name = _name;
    student.class = _class;
    student.score = _score;
    studentList.push(student);
  }
  function get(uint _index) public view returns (Student memory) {
    return studentList[_index];
  }
  //設定姓名
  function setName(uint _index, string memory _name) public {
    require(_index < studentList.length, "index must be less than the group maximum");//驗證
    Student storage student = studentList[_index];
    student.name = _name;
  }
  //設定成績
  function setScore(uint _index, uint _score) public {
    require(_index < studentList.length, "index must be less than the group maximum");//驗證
    Student storage student = studentList[_index];
    student.score = _score;
  }
  //顯示整個student group
  function showStudentGroup() public view returns (Student[] memory) {
    return studentList;
  }
}
從以上的介紹就可以看出,有了struct後,要用來顯示student的資訊和群組就會想對來的容易了,不能分成多個參數,方便程式碼的識別,以上是對struct的介紹,謝謝。
有什麼想要了解或是不清楚的部份,歡迎留言和我分享。
如果喜歡我的文章歡迎追隨,按愛心,我每週都會上新文章。
為什麼會看到廣告
宅D嘉
宅D嘉
有著十年以上的軟體開發經驗,喜歡投資理財,目前喜歡研究區塊鏈相關,了解虛擬貨幣投資或者是區塊鏈開發,喜歡把複雜的東西簡單化,樂於分享我的理財投資生活。
留言0
查看全部
發表第一個留言支持創作者!