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.

    }

}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值