如何理解external

external

  • 函数应该只被外部函数调用
  • 但也可以被内部调用,但是这种内部调用也是有外部调用机制,即新产生message!

例子1

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract ExternalDemo{
    
    
     
    address public caller;
    function first() public{
    
    
        second();
    }
   function second() public {
    
    
        caller = msg.sender;
    }
}
// 外部测试合约地址:0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

// 当前合约地址: 0xcD6a42782d230D7c13A74ddec5dD140e55499Df9

效果1
**加粗样式**

列子2

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract ExternalDemo{
    
    
     
    address public caller;

    function first() public{
    
    
        this.second();  
    }
   function second() external{
    
    
        caller = msg.sender;
    }
}
// 外部测试合约地址:0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

// 当前合约地址: 0xD4Fc541236927E2EAf8F27606bD7309C1Fc2cbee

效果:直接触发first的时候,caller为当前合约地址
在这里插入图片描述
效果:直接触发second时,caller为外部测试合约地址
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37117521/article/details/139553826