Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the inheritance of solidity

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the inheritance of solidity". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the inheritance of solidity".

In Solidity, inheritance is very similar to a classic object-oriented programming language. You first write a basic intelligence contract and inform you that your new intelligence contract will be inherited from the base contract.

You must also understand that Solidity supports multiple inheritance by copying code that contains polymorphisms. All function calls are virtual functions, which means that they will be the ones that call the most derived functions, unless the contract name is explicitly given. When a smart contract inherits from multiple contracts, only one smart contract is created on the block chain, and the code from all the underlying contracts is copied to the created smart contract.

Let's write down our basic smart contract: it will make it easy for us to add ownership to our contract. We call it Ownable. OpenZeppelin employees have written a lot of reusable code that can be used in smart contracts. These code snippets are available through its tools or its Github repository.

This is the code:

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

Another pattern we often write about is the ability to break our contract and transfer the money stored in the contract to the owner or another address. The important thing is that we don't want anyone to break our contract, so our Destructible should inherit Ownable. Inheritance is done using the is keyword after the smart contract name.

It must be noted that it is Solidity, a function by default, or accessible from a derived class. As with other programming languages, you can specify what can be accessed from external or derived contracts. The function can be specified as external,public,internal,private, and the default is public.

External: external functions are part of the smart contract interface, which means they can be called from other contracts and transactions. The external function f cannot be called internally (that is, f () does not work, but this.f () does). When external functions receive large amounts of data, they are sometimes more efficient.

Public: public functions are part of the intelligent contract interface and can be called internally or via messages. For public state variables, an automatic getter function is generated (see below).

Internal: these functions and state variables can only be accessed internally (that is, from or derived from the current contract), while it is not used in other cases.

Private: private functions and state variables are visible only to the smart contract that defines them, not in the derived contract.

Here is our second smart contract:

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

Now using these two basic contracts, we will write a simple BankAccount smart contract that people can remit and owners can withdraw.

Pragma solidity ^ 0.4.11 X contract BankAccount is Ownable, Destructible {function store () public payable {} function withdraw (uint amount) public onlyOwner {if (this.balance > = amount) {msg.sender.transfer (amount);}

Please note that we need to inherit from two smart contracts. The order of inheritance is important. A simple rule for determining the order is to specify the base class in the order from "most similar to base class" to "most derived".

Here is the entire code that we will deploy:

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

We can now deploy our bank account bank account smart contract.

After deployment, we can see that we see our bank account functionality, but also see the inherited functionality.

Thank you for your reading, the above is the content of "how to use the inheritance of solidity", after the study of this article, I believe you have a deeper understanding of how to use the inheritance of solidity, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report