本篇记录 solidity 合约中,子合约如何初始化父合约的参数;及在初始化多个父合约时的调用顺序;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
// Base contract X
contract X {
string public name;
constructor(string memory _name) {
name = _name;
}
}
// Base contract Y
contract Y {
string public text;
constructor(string memory _text) {
text = _text;
}
}
// There are 2 ways to initialize parent contract with parameters.
// Pass the parameters here in the inheritance list.
contract B is X("Input to X"), Y("Input to Y") {
}
contract C is X, Y {
// Pass the parameters here in the constructor,
// similar to function modifiers.
constructor(string memory _name, string memory _text) X(_name) Y(_text) {}
}
// Parent constructors are always called in the orde
这篇笔记探讨了在Solidity编程中,如何在子合约创建时正确设置父合约的初始化参数,同时详细阐述了当存在多个父合约继承时的调用顺序。对于理解和实践智能合约开发具有指导意义。
订阅专栏 解锁全文
559

被折叠的 条评论
为什么被折叠?



