英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论