【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 6 | 資料結構 Data Structures

【Structs】

宣告 struct 在這裡的用途和 C 的語言一樣,如果是沒有學過 C/C++ 的話,可以把 struct 理解成一種比較低階的 C++, python 的 class。

定義一個 struct 之後會產生一種新的自定義、抽象、複雜的的資料型態。後我們就可以藉由這個資料型態宣告我們自己的結構變數。

struct User {
  address id;
  string name;
}

//Method 1 (argument order matters)
User("0xAio90....", "Mike");

//Method 2 (argument order does not matter)
User({name: "Mike", id: "0xAio90...."});

就像是我們定義一個新的形態是車子,並且定義一台車子裡面要有的資料有:持有者(address)、品牌(string)、公升數(uint)。

此後就可以宣告一個資料型態為車子的變數 myCar,並定義這個變數裡面的每個型態。

struct Car {
  address owner;
  string brand;
  uint cc;
}

myCar = Car({string: "Toyota", address: "0xAio90....", cc: 2000});

Structs具有以下的性質:

  • 在Solidity中不可以使用遞迴性的方式宣告變數,也就是說struct的成員的型別不可是該struct。

  • 以structs取代objects可以達到gas consumption

  • 我們可以組合struct和mapping或甚至其他容器來建置些複雜的資料結構,像是以下例子是使用巢狀的資料結構(Nested Data Structures)來達成目的。

struct User {
  uint id;
  string name;
}

uint[] userIds;

mapping(uint => User) users;

其他範例:

pragma solidity ^0.8.11;

contract complexVariable {

    struct Payment {
        uint amount;
        uint timestamp;
    } // uppercase -> type; lowercase -> variable

    struct Balance {
        uint totalBalance;
        uint numPayments;
        mapping(uint => Payment) payments;
    }
    
    mapping(address => Balance) public balanceReveived;
    
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
    
    function sendMoney() public payable {
        balanceReveived[msg.sender].totalBalance += msg.value;
        
        Payment memory payment = Payment(msg.value, block.timestamp);
        
        balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment;
        balanceReceived[msg.sender].numPayments++;
    }
    
    function withdrawMoney(address payable _to, uint _amount) public {
        require(balanceReceived[msg.sender].totalBalance <= _amount, "not enough funds");
        balanceReceived[msg.sender].totalBalance -= _amount;
        _to.transfer(_amount);
    }
    
    function withdrawAllMoney(address payable _to) public {
        uint balanceToSend = balanceReceived[msg.sender].totalBalance;
        balanceReveived[msg.sender].totalBalance = 0;
        _to.transfer(balanceToSend);   
    }
}
Previous【Mapping】Next【Enum】

Last updated 3 years ago

Was this helpful?