solidity語言開發智能合約中的繼承

我們已經探索了很多主題,在編寫智能合約時我們發現經常使用相同的模式:例如,智能合約具有在構造函數中設置的所有者,然後生成修改器以便僅讓所有者使用一些功能。如果我們制定實施這些功能的基礎合約並在未來的智能合約中重複使用它們那該怎麼辦?你一定猜得到,我們將使用繼承。

在Solidity中,繼承與經典的面向對象編程語言非常相似。你首先編寫基本智能合約並告知你的新智能合約將從基礎合約繼承。

你還必須通過複製包含多態的代碼來了解Solidity支持多重繼承。所有函數調用都是虛函數,這意味着會是調用派生函數最多的函數,除非明確給出了合約名稱。當某一個智能合約從多個合約繼承時,只在區塊鏈上創建一個智能合約,並將所有基礎合約中的代碼複製到創建的智能合約中。

讓我們寫下我們的基本智能合約:它將讓我們輕鬆地爲我們的合約添加所有權。我們將其命名爲OwnableOpenZeppelin的員工寫了很多可以在智能合約中使用的可重用代碼。這些代碼段可通過其工具或其Github存儲庫獲得。

這是代碼:

pragma solidity ^0.4.11;

/**
 * @title Ownable
 * @dev The Ownable contract has an owner address, and provides basic authorization control
 * functions, this simplifies the implementation of "user permissions".
 */
contract Ownable {

  address public owner;

  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  function Ownable() {
    owner = msg.sender;
  }


  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }


  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) onlyOwner {
    require(newOwner != address(0));      
    owner = newOwner;
  }

}

我們經常寫的另一種模式是破壞我們的合約並將合約中存儲的資金轉移給所有者或另一個地址的能力。重要的是我們不希望任何人能夠破壞我們的合約,所以我們的Destructible應該繼承Ownable。繼承是使用智能合約名稱後面的is關鍵字完成的。

必須注意,它是Solidity,默認情況下是函數,或者可以從派生類訪問。與其他編程語言一樣,你可以指定從外部或派生合約中可以訪問的內容。函數可以指定爲externalpublicinternalprivate,默認爲public

  • external:外部函數是智能合約接口的一部分,這意味着可以從其他合約和交易中調用它們。external函數f不能在內部調用(即f()不起作用,但this.f()起作用)。當外部函數接收大量數據時,它們有時會更有效。
  • public:公共函數是智能合約接口的一部分,可以在內部調用,也可以通過消息調用。對於公共狀態變量,會生成自動getter函數(見下文)。
  • internal:這些函數和狀態變量只能在內部訪問(即從當前合約或從中派生的合約中),而其他情況不使用它。
  • private:私有函數和狀態變量僅對定義它們的智能合約可見,而不是在派生合約中可見。

下面是我們的第二份智能合約:

pragma solidity ^0.4.11;

/**
 * @title Destructible
 * @dev Base contract that can be destroyed by owner. All funds in contract will be sent to the owner.
 */
contract Destructible is Ownable {

  function Destructible() payable { } 

  /**
   * @dev Transfers the current balance to the owner and terminates the contract. 
   */
  function destroy() onlyOwner {
    selfdestruct(owner);
  }

  function destroyAndSend(address _recipient) onlyOwner {
    selfdestruct(_recipient);
  }
}

現在使用這兩個基本合約,我們將寫一個簡單的BankAccount智能合約,人們可以匯款,業主可以提取。

pragma solidity ^0.4.11;

contract BankAccount is Ownable, Destructible {

  function store() public payable {
      
  } 

  function withdraw(uint amount) public onlyOwner {
      if (this.balance >= amount) {
        msg.sender.transfer(amount);
      }
  } 

}

請注意,我們需要從兩個智能合約繼承。繼承的順序很重要。判斷順序的一個簡單規則是按照“最類似基類”到“最多派生”的順序指定基類。

以下是我們將部署的整個代碼:

pragma solidity ^0.4.11;

/**
 * @title Ownable
 * @dev The Ownable contract has an owner address, and provides basic authorization control
 * functions, this simplifies the implementation of "user permissions".
 */
contract Ownable {

  address public owner;

  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  function Ownable() {
    owner = msg.sender;
  }


  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }


  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) onlyOwner {
    require(newOwner != address(0));      
    owner = newOwner;
  }

}

/**
 * @title Destructible
 * @dev Base contract that can be destroyed by owner. All funds in contract will be sent to the owner.
 */
contract Destructible is Ownable {

  function Destructible() payable { } 

  /**
   * @dev Transfers the current balance to the owner and terminates the contract. 
   */
  function destroy() onlyOwner {
    selfdestruct(owner);
  }

  function destroyAndSend(address _recipient) onlyOwner {
    selfdestruct(_recipient);
  }
}

contract BankAccount is Ownable, Destructible {

  function store() public payable {
      
  } 

  function withdraw(uint amount) public onlyOwner {
      if (this.balance >= amount) {
        msg.sender.transfer(amount);
      }
  } 

}

我們現在可以部署我們的銀行賬戶bank account智能合約了。

部署後,我們可以看到我們看到了我們的銀行帳戶功能,但也看到了繼承的功能。

======================================================================

分享一些以太坊、EOS、比特幣等區塊鏈相關的交互式在線編程實戰教程:

  • java以太坊開發教程,主要是針對java和android程序員進行區塊鏈以太坊開發的web3j詳解。
  • python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
  • php以太坊,主要是介紹使用php進行智能合約開發交互,進行賬號創建、交易、轉賬、代幣開發以及過濾器和交易等內容。
  • 以太坊入門教程,主要介紹智能合約與dapp應用開發,適合入門。
  • 以太坊開發進階教程,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
  • C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括賬戶管理、狀態與交易、智能合約開發與交互、過濾器和交易等。
  • EOS教程,本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、賬戶與錢包、發行代幣、智能合約開發與部署、使用代碼與智能合約交互等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
  • java比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Java代碼中集成比特幣支持功能,例如創建地址、管理錢包、構造裸交易等,是Java工程師不可多得的比特幣開發學習課程。
  • php比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Php代碼中集成比特幣支持功能,例如創建地址、管理錢包、構造裸交易等,是Php工程師不可多得的比特幣開發學習課程。
  • tendermint區塊鏈開發詳解,本課程適合希望使用tendermint進行區塊鏈開發的工程師,課程內容即包括tendermint應用開發模型中的核心概念,例如ABCI接口、默克爾樹、多版本狀態庫等,也包括代幣發行等豐富的實操代碼,是go語言工程師快速入門區塊鏈開發的最佳選擇。

這裏是原文solidity語言開發中的繼承

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章