Solidity 044 Interface

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

// Define an interface IHelloWorld. Interfaces are used to define contracts' external functions without implementing them.

interface IHelloWorld {

   // Declare an external function GetValue that, when implemented, will return a uint value.

   function GetValue() external view returns (uint);

   // Declare an external function SetValue that takes a uint _value as input.

   function SetValue(uint _value) external;

}

// Implement the IHelloWorld interface with a contract named HelloWorld.

contract HelloWorld is IHelloWorld {

  // Declare a private uint variable to store a simple integer value.

  uint private simpleInteger;

  // Implement the GetValue function defined in the IHelloWorld interface.

  // This external view function returns the current value of simpleInteger.

  function GetValue() external view override returns (uint) {

    return simpleInteger;

  }

  // Implement the SetValue function defined in the IHelloWorld interface.

  // This external function updates the value of simpleInteger.

  function SetValue(uint _value) external override {

   simpleInteger = _value;

  }

}

// Define a contract named Client to demonstrate interaction with the HelloWorld contract via the IHelloWorld interface.

contract Client {

  // Define a public function that demonstrates setting and getting a value from the HelloWorld contract.

  function GetSetIntegerValue() public returns (uint) {

    // Instantiate a new HelloWorld contract and assign it to the interface type IHelloWorld.

    IHelloWorld myObj = new HelloWorld();

   

    // Set the value of simpleInteger in the HelloWorld contract to 100.

    myObj.SetValue(100);

   

    // Get the current value of simpleInteger from the HelloWorld contract, add 10 to it, and return the result.

    // Note: The original approach will work, but typically you'd interact with the contract directly

    // if it's being created within the same transaction for efficiency.

    return myObj.GetValue() + 10;

  }

}

Solidity 044 Interface_第1张图片

你可能感兴趣的:(Solidity,金融,区块链,智能合约,分布式账本,信任链)