溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

solidity的繼承怎么使用

發(fā)布時(shí)間:2021-12-07 15:25:57 來(lái)源:億速云 閱讀:251 作者:iii 欄目:互聯(lián)網(wǎng)科技

這篇文章主要講解了“solidity的繼承怎么使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“solidity的繼承怎么使用”吧!

在Solidity中,繼承與經(jīng)典的面向?qū)ο缶幊陶Z(yǔ)言非常相似。你首先編寫基本智能合約并告知你的新智能合約將從基礎(chǔ)合約繼承。

你還必須通過(guò)復(fù)制包含多態(tài)的代碼來(lái)了解Solidity支持多重繼承。所有函數(shù)調(diào)用都是虛函數(shù),這意味著會(huì)是調(diào)用派生函數(shù)最多的函數(shù),除非明確給出了合約名稱。當(dāng)某一個(gè)智能合約從多個(gè)合約繼承時(shí),只在區(qū)塊鏈上創(chuàng)建一個(gè)智能合約,并將所有基礎(chǔ)合約中的代碼復(fù)制到創(chuàng)建的智能合約中。

讓我們寫下我們的基本智能合約:它將讓我們輕松地為我們的合約添加所有權(quán)。我們將其命名為Ownable。OpenZeppelin的員工寫了很多可以在智能合約中使用的可重用代碼。這些代碼段可通過(guò)其工具或其Github存儲(chǔ)庫(kù)獲得。

這是代碼:

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;
  }

}

我們經(jīng)常寫的另一種模式是破壞我們的合約并將合約中存儲(chǔ)的資金轉(zhuǎn)移給所有者或另一個(gè)地址的能力。重要的是我們不希望任何人能夠破壞我們的合約,所以我們的Destructible應(yīng)該繼承Ownable。繼承是使用智能合約名稱后面的is關(guān)鍵字完成的。

必須注意,它是Solidity,默認(rèn)情況下是函數(shù),或者可以從派生類訪問。與其他編程語(yǔ)言一樣,你可以指定從外部或派生合約中可以訪問的內(nèi)容。函數(shù)可以指定為external,public,internal,private,默認(rèn)為public。

  • external:外部函數(shù)是智能合約接口的一部分,這意味著可以從其他合約和交易中調(diào)用它們。external函數(shù)f不能在內(nèi)部調(diào)用(即f()不起作用,但this.f()起作用)。當(dāng)外部函數(shù)接收大量數(shù)據(jù)時(shí),它們有時(shí)會(huì)更有效。

  • public:公共函數(shù)是智能合約接口的一部分,可以在內(nèi)部調(diào)用,也可以通過(guò)消息調(diào)用。對(duì)于公共狀態(tài)變量,會(huì)生成自動(dòng)getter函數(shù)(見下文)。

  • internal:這些函數(shù)和狀態(tài)變量只能在內(nèi)部訪問(即從當(dāng)前合約或從中派生的合約中),而其他情況不使用它。

  • private:私有函數(shù)和狀態(tài)變量?jī)H對(duì)定義它們的智能合約可見,而不是在派生合約中可見。

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

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);
  }
}

現(xiàn)在使用這兩個(gè)基本合約,我們將寫一個(gè)簡(jiǎn)單的BankAccount智能合約,人們可以匯款,業(yè)主可以提取。

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);
      }
  } 

}

請(qǐng)注意,我們需要從兩個(gè)智能合約繼承。繼承的順序很重要。判斷順序的一個(gè)簡(jiǎn)單規(guī)則是按照“最類似基類”到“最多派生”的順序指定基類。

以下是我們將部署的整個(gè)代碼:

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);
      }
  } 

}

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

solidity的繼承怎么使用

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

solidity的繼承怎么使用

感謝各位的閱讀,以上就是“solidity的繼承怎么使用”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)solidity的繼承怎么使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI