咱們已經探索了不少主題,在編寫智能合約時咱們發現常常使用相同的模式:例如,智能合約具備在構造函數中設置的全部者,而後生成修改器以便僅讓全部者使用一些功能。若是咱們制定實施這些功能的基礎合約並在將來的智能合約中重複使用它們那該怎麼辦?你必定猜獲得,咱們將使用繼承。php
在Solidity中,繼承與經典的面向對象編程語言很是類似。你首先編寫基本智能合約並告知你的新智能合約將從基礎合約繼承。java
你還必須經過複製包含多態的代碼來了解Solidity支持多重繼承。全部函數調用都是虛函數,這意味着會是調用派生函數最多的函數,除非明確給出了合約名稱。當某一個智能合約從多個合約繼承時,只在區塊鏈上建立一個智能合約,並將全部基礎合約中的代碼複製到建立的智能合約中。node
讓咱們寫下咱們的基本智能合約:它將讓咱們輕鬆地爲咱們的合約添加全部權。咱們將其命名爲Ownable
。OpenZeppelin的員工寫了不少能夠在智能合約中使用的可重用代碼。這些代碼段可經過其工具或其Github存儲庫得到。python
這是代碼:android
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
關鍵字完成的。git
必須注意,它是Solidity,默認狀況下是函數,或者能夠從派生類訪問。與其餘編程語言同樣,你能夠指定從外部或派生合約中能夠訪問的內容。函數能夠指定爲external
,public
,internal
,private
,默認爲public
。程序員
external
:外部函數是智能合約接口的一部分,這意味着能夠從其餘合約和交易中調用它們。external
函數f不能在內部調用(即f()不起做用,但this.f()起做用)。當外部函數接收大量數據時,它們有時會更有效。public
:公共函數是智能合約接口的一部分,能夠在內部調用,也能夠經過消息調用。對於公共狀態變量,會生成自動getter函數(見下文)。internal
:這些函數和狀態變量只能在內部訪問(即從當前合約或從中派生的合約中),而其餘狀況不使用它。private
:私有函數和狀態變量僅對定義它們的智能合約可見,而不是在派生合約中可見。下面是咱們的第二份智能合約:github
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
智能合約,人們能夠匯款,業主能夠提取。web
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); } } }
請注意,咱們須要從兩個智能合約繼承。繼承的順序很重要。判斷順序的一個簡單規則是按照「最相似基類」到「最多派生」的順序指定基類。mongodb
如下是咱們將部署的整個代碼:
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
智能合約了。
部署後,咱們能夠看到咱們看到了咱們的銀行賬戶功能,但也看到了繼承的功能。
分享一些以太坊相關的交互式在線編程實戰教程:
- java以太坊開發教程,主要是針對java和android程序員進行區塊鏈以太坊開發的web3j詳解。
- python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
- php以太坊,主要是介紹使用php進行智能合約開發交互,進行帳號建立、交易、轉帳、代幣開發以及過濾器和交易等內容。
- 以太坊入門教程,主要介紹智能合約與dapp應用開發,適合入門。
- 以太坊開發進階教程,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
- C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括帳戶管理、狀態與交易、智能合約開發與交互、過濾器和交易等。
這裏是原文solidity語言開發中的繼承