solidity語言開發中的繼承
我們已經探索了很多主題,在編寫智慧合約時我們發現經常使用相同的模式:例如,智慧合約具有在建構函式中設定的所有者,然後生成修改器以便僅讓所有者使用一些功能。如果我們制定實施這些功能的基礎合約並在未來的智慧合約中重複使用它們那該怎麼辦?你一定猜得到,我們將使用繼承。
在Solidity中,繼承與經典的面向物件程式語言非常相似。你首先編寫基本智慧合約並告知你的新智慧合約將從基礎合約繼承。
你還必須通過複製包含多型的程式碼來了解Solidity支援多重繼承。所有函式呼叫都是虛擬函式,這意味著會是呼叫派生函式最多的函式,除非明確給出了合約名稱。當某一個智慧合約從多個合約繼承時,只在區塊鏈上建立一個智慧合約,並將所有基礎合約中的程式碼複製到建立的智慧合約中。
讓我們寫下我們的基本智慧合約:它將讓我們輕鬆地為我們的合約新增所有權。我們將其命名為 Ownable
。 ofollow,noindex">OpenZeppelin 的員工寫了很多可以在智慧合約中使用的可重用程式碼。這些程式碼段可通過其工具或其 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,預設情況下是函式,或者可以從派生類訪問。與其他程式語言一樣,你可以指定從外部或派生合約中可以訪問的內容。函式可以指定為 external
, public
, internal
, private
,預設為 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語言開發中的繼承