【試讀版】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 II Medium
  2. | Chapter 11 | 繼承 Inheritance

【多重繼承與 super】

當一個合約從多個合約繼承時,在區塊鏈上只會有一個合約被建立,所有父合約的程式碼都被編譯到我們建立的子合約中。

在多重繼承的情況下 super 這個關鍵字在 Solidity 中可以調用最初的父合約。使用 super 的函數調用優先於大多數派生合約。

當我們擁有一個 contract A 並在其中有一個函數 f(),並且它的父合約之中也有一個函數 f()。此時 A 會覆寫(overrides)B 的 f()。這代表 myInstanceOfA.f() 會呼叫 A 之中的 f(),且 B 之中的 f() 將不可再被調用,但如果我們想要調用合約 B 的 f() 則可以再 A 之中使用 super.f()。

或者我們也可以顯式地(explicitly)宣告父合約的函數。以下是多重繼承的例子:

首先建立一個 c 合約,這個合約定義了一個變數 u。然後 b 合約去繼承 c 合約。這裡就不要定義變量了,使用的時 c 合約的變數。然後 a 合約繼承了 b 合約。

pragma solidity ^0.8.11;

contract C {
  uint public u;
  function f() public virtual {
    u = 1;
  }
}

contract B is C {
  function f() public virtual override{
    u = 2;
  }
}

contract A is B {
  function f() public override{  // will set u to 3
    u = 3;
  }
  function f1() public { // will set u to 2
    super.f();
  }
  function f2() public { // will set u to 2
    B.f();
    // 使用 super 去呼叫的話,是呼叫 b 合約的,而不是 c 合約的。
  }
  function f3() public { // will set u to 1
    C.f();
  // 如果要呼叫 c 合約中的函式,需要使用函式名。
  }
}

需要注意的是父合約的 State Variables 不可以在子合約被更改,同時也不可以在子合約宣告在父合約們中已經宣告同樣名稱的 State Variables。

Previous【Polymorphism】Next【Practice】

Last updated 3 years ago

Was this helpful?