Solidity构造函数语法如何工作?

huangapple go评论48阅读模式
英文:

How Does Solidity Constructor Syntax Work?

问题

I'm having trouble understanding the syntax of the following line of Solidity used to construct an ERC20 token using the OpenZepplin library:

constructor(uint256 cap) ERC20("DevToken", "DVT") ERC20Capped(cap){ }

I'm looking for clarification of the following points:

  • Why does the constructor seemingly have two names; 'ERC20' and 'ERC20Capped'?
  • Why does the ERC20Capped portion take a 'cap' variable instead of actual data like the ERC20 portion does?

If someone is able to go through word by word and explain the entire line that would be even more helpful.

I find the documentation for solidity very hard to understand, as an aside, does anyone have any recommendations for resources that will help me better understand the syntax of the language?

英文:

I'm having trouble understanding the syntax of the following line of Solidity used to construct an ERC20 token using the OpenZepplin library:

constructor(uint256 cap) ERC20("DevToken", "DVT") ERC20Capped(cap){ }

I'm looking for clarification of the following points:

  • Why does the constructor seemingly have two names; 'ERC20' and 'ERC20Capped'?
  • Why does the ERC20Capped portion take a 'cap' variable instead of actual data like the ERC20 portion does?

If someone is able to go through word by word and explain the entire line that would be even more helpful.

I find the documentation for solidity very hard to understand, as an aside, does anyone have any recommendations for resources that will help me better understand the syntax of the language?

答案1

得分: 1

  • 构造函数没有任何名称。合同的constructor仅在部署合同时调用

您的基本合同继承自其他两个合同:

ERC20("DevToken", "DVT") ERC20Capped(cap)

当一个合同从另一个合同继承时,它可以访问继承合同的函数、变量和修饰符。在部署过程中,基本合同的字节码包括继承合同的字节码。这创建了一个单一的字节码文件,代表了部署合同及其继承合同的完整功能。因为继承的合同有constructor函数,所以在部署合同时,您也会初始化这些构造函数。

  • 为什么ERC20Capped部分需要一个'cap'变量,而不是像ERC20部分一样的实际数据?

您应该检查继承的合同,它们会告诉您传递给它们构造函数的参数。

英文:

> - constructor does not have any names. constructor of the contract is only called when you deploy the contract

Your base contract is inheriting from two other contracts:

ERC20("DevToken", "DVT") ERC20Capped(cap)

when a contract inherits from another contract, it gains access to the inherited contract's functions, variables, and modifiers. During deployment, the bytecode of the base contract includes the bytecode of the inherited contracts. This creates a single bytecode file that represents the complete functionality of the deploying contract and its inherited contracts. since inherited contracts have constructor function, when you deploy your contract, you are initializing those constructors, as well.

> - Why does the ERC20Capped portion take a 'cap' variable instead of actual data like the ERC20 portion does?

you should check inherited contracts and they will tell you what parameters are passed to their constructors

huangapple
  • 本文由 发表于 2023年5月21日 22:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定