坚固的构造函数语法
浏览openzeppelin教程,我遇到了此代码片段:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract GameItem {
constructor() ERC721("GameItem", "ITM") {}
构造函数中的语法是什么,可以在constructor()之后传递类实例?我似乎在坚固的文档中找不到对此的任何参考
Looking through an OpenZeppelin tutorial, I came across this code snippet:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract GameItem {
constructor() ERC721("GameItem", "ITM") {}
What is the syntax in the constructor that allows a class instance to be passed in after constructor()? I can’t seem to find any reference to this in the Solidity docs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它正在调用父构建器,在这种情况下,父类被命名为
erc721
。它与调用super.constructor()
或parent.constructor()
中的其他一些语言相同。为了致电父构建器,您的合同实际上需要从中继承:
坚固性支持多个父母的继承。这就是为什么您不能仅使用模棱两可的关键字,例如
parent
。通过明确说明父级名称,您可以指定要传递到哪个父的值:docs: https://docs.solitylang.org/en/v0.8.13/contracts.html#arguments-for-base-constructors
It's invoking a parent constructor, in this case the parent class is named
ERC721
. It's the same as callingsuper.constructor()
orparent.constructor()
in some other languages.In order to call the parent constructor, your contract needs to actually inherit from it:
Solidity supports inheritance from multiple parents. That's why you cannot just use an ambiguous keyword such as
parent
. By explicitly stating the parent class name, you can specify which values you want to pass to which parent:Docs: https://docs.soliditylang.org/en/v0.8.13/contracts.html#arguments-for-base-constructors