1. 程式人生 > >Solidify實現一個智慧合約11(結構體)

Solidify實現一個智慧合約11(結構體)

結構體的宣告

pragma solidity ^0.4.4;

contract Students {

  struct Person {
    uint age;
    uint stuId;
    string name;
  }

  Person[] _person = new Person[](5);
}

結構體的初始化

pragma solidity ^0.4.4;

contract Students {

  struct Person {
    uint age;
    uint stuId;
    string name;
  }
  //建立物件陣列
  Person[] persons = new Person[](3); 
  //第一種賦值方式
  Person _person1 = Person(18,10001,"Hisee");
  Person _person2 = Person(21,10002,"Hello");
  //第二種賦值方式
  Person _person3 = Person({age:18,stuId:10003,name:"His"});
  
  function Students() {
    persons[0] = _person1;
    persons[1] = _person2;
    persons[2] = _person3;
  }

  function f() {
    //=前半是storage型別,=後半是memory型別,編譯會報錯,需將前半改為memory型別。
    Person memory person1 = Person(18,10004,"xiaobai"); 
  }
}

注意:在函式內部時,=前半是storage型別,=後半是memory型別,編譯會報錯,需將前半改為memory型別。在函式外部時,都為storage型別。