solidity语言开发以太坊智能合约中的继承

咱们已经探索了不少主题,在编写智能合约时咱们发现常常使用相同的模式:例如,智能合约具备在构造函数中设置的全部者,而后生成修改器以便仅让全部者使用一些功能。若是咱们制定实施这些功能的基础合约并在将来的智能合约中重复使用它们那该怎么办?你必定猜获得,咱们将使用继承。php

在Solidity中,继承与经典的面向对象编程语言很是类似。你首先编写基本智能合约并告知你的新智能合约将从基础合约继承。java

你还必须经过复制包含多态的代码来了解Solidity支持多重继承。全部函数调用都是虚函数,这意味着会是调用派生函数最多的函数,除非明确给出了合约名称。当某一个智能合约从多个合约继承时,只在区块链上建立一个智能合约,并将全部基础合约中的代码复制到建立的智能合约中。node

让咱们写下咱们的基本智能合约:它将让咱们轻松地为咱们的合约添加全部权。咱们将其命名为OwnableOpenZeppelin的员工写了不少能够在智能合约中使用的可重用代码。这些代码段可经过其工具或其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,默认状况下是函数,或者能够从派生类访问。与其余编程语言同样,你能够指定从外部或派生合约中能够访问的内容。函数能够指定为externalpublicinternalprivate,默认为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语言开发中的继承

相关文章
相关标签/搜索