外部功能可以在坚固的派生合同中起作用吗?

发布于 2025-01-20 17:13:22 字数 892 浏览 1 评论 0原文

我在合同a中具有可见性外部的功能,但是当我尝试通过派生的合同B访问时,它会出现错误。

代码:

contract A{

      function f1() pure public  returns(uint) { 
       return 1;
    }

      function f2() pure private returns(uint) { 
       return 2;
    } 
     
    function f3() pure internal returns(uint){   
        return 3;
    }

    function f4() pure external returns(uint){ 
        return 4;
    }
}

contract B is A{ 
   
     uint bx = f3();  
}


“错误image”

如果答案是“是”,请说明原因。然后,为什么它在合同C中工作。是因为它不是派生的合同吗?说明这也是原因。

I made a function with visibility external in a contract A but When I tried to access through a derived contract B, it's giving an error.

Code:

contract A{

      function f1() pure public  returns(uint) { 
       return 1;
    }

      function f2() pure private returns(uint) { 
       return 2;
    } 
     
    function f3() pure internal returns(uint){   
        return 3;
    }

    function f4() pure external returns(uint){ 
        return 4;
    }
}

contract B is A{ 
   
     uint bx = f3();  
}


Error Image

If the answer is yes, state the reason. And then why it's working in contract C. Is it because it's not a derived contract?. State it's reason too.

Contract C image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2025-01-27 17:13:22

Solidity 文档给出了您问题的答案,我无需解释太多。有关其他可见性,请参阅 文档

Solidity 知道两种函数调用:创建实际 EVM 消息调用的外部调用和不创建实际 EVM 消息调用的内部调用。此外,可以使派生合约无法访问内部函数。这产生了四种类型的函数可见性。

外部函数是合约接口的一部分,这意味着它们可以从其他合约和交易中调用。外部函数 f 不能在内部调用(即 f() 不起作用,但 this.f() 起作用)。

The solidity documentation gives a spot on answer for your question, leaving me not much to explain. See docs for other visibilities

Solidity knows two kinds of function calls: external ones that do create an actual EVM message call and internal ones that do not. Furthermore, internal functions can be made inaccessible to derived contracts. This gives rise to four types of visibility for functions.

External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文