第121篇 笔记-初始化父合约的参数

Posted wonderBlock

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第121篇 笔记-初始化父合约的参数相关的知识,希望对你有一定的参考价值。

本篇记录 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 order of inheritance
// regardless of the order of parent contracts listed in the
// constructor of the child contract.

// Order of constructors called:
// 1.

以上是关于第121篇 笔记-初始化父合约的参数的主要内容,如果未能解决你的问题,请参考以下文章