solidity:4.函数可见性与修饰符

一. 函数可见性

  • public - 支持内部或外部调用
  • private - 仅在当前合约调用,且不可被继承
  • internal- 只支持内部调用
  • external - 不支持内部调用
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

contract VisibilityA{

   uint public x;

   function t1() public pure returns(string memory) {
      return "public";
   }

   function t2() external pure returns(string memory) {
      return "external";
   }

    function t3() internal pure returns(string memory) {
      return "internal";
   }

    function t4() private pure returns(string memory) {
      return "private";
   }

    function t5() public view {
       t1();
       this.t2();  //可通过this调用外部函数
       t3();
       t4();
   }
}

contract VisibilityB is VisibilityA{
    function t6() public pure returns(string memory) {
       // t4(); private在继承时不可见
       // t2(); external在继承时不可见
       return t1();
    }

     function t7() public pure returns(string memory) {
        return t3();
    }
}

二. 函数修饰符

  • view - 表示该函数只读取而不修改状态变量
  • pure - 表示该函数既不读取也不修改状态变量
pragma solidity ^0.8.0;

contract ViewAndPure {
    uint x;

    function getA()public view returns(uint) {
       return x;
    } 

    function getB()public pure returns(uint) {
        return 3;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值