Solidity 043 Reheritance

// SPDX-License-Identifier: MIT

// Declare the version of Solidity the contracts are written in.

pragma solidity ^0.8.9;

// Define a parent contract named ParentContract.

contract ParentContract {

    // Declare an internal uint variable. Internal variables can be accessed

    // within the contract itself and by contracts that inherit from it.

    uint internal simpleInteger;

    // Define a function to set the value of simpleInteger.

    // External functions are part of the contract interface and can only be called from other contracts and transactions.

    function SetInteger(uint _value) external {

        simpleInteger = _value;

    }

}

// Define a child contract named ChildContract that inherits from ParentContract.

contract ChildContract is ParentContract {

    // Declare a private bool variable. Private variables are only visible for the contract they are defined in.

    bool private simpleBool;

    // Define a public function that returns the value of simpleInteger.

    // Public functions can be called both internally and via transactions.

    function GetInteger() public view returns (uint) {

        return simpleInteger;

    }

}

// Define a contract named Client to interact with ChildContract.

contract Client {

    // Instantiate a new ChildContract object.

    ChildContract pc = new ChildContract();

    // Define a public function that demonstrates how to work with inheritance.

    // It sets a value for simpleInteger through the ChildContract and retrieves it.

    function workWithInheritance() public returns (uint) {

        pc.SetInteger(100); // Set the simpleInteger of ChildContract to 100.

        return pc.GetInteger(); // Return the value of simpleInteger from ChildContract.

    }

}

Solidity 043 Reheritance_第1张图片

你可能感兴趣的:(Solidity,金融,区块链,智能合约,信任链,去中心化)