📜  Solidity-函数重载

📅  最后修改于: 2020-11-04 04:27:20             🧑  作者: Mango


 

在同一个作用域中,可以为同一个函数名具有多个定义。函数的定义必须在参数列表中的参数类型和/或数量上彼此不同。您不能重载仅在返回类型上有所不同的函数声明。

以下示例显示了Solidity中函数重载的概念。

pragma solidity ^0.5.0;

contract Test {
   function getSum(uint a, uint b) public pure returns(uint){      
      return a + b;
   }
   function getSum(uint a, uint b, uint c) public pure returns(uint){      
      return a + b + c;
   }
   function callSumWithTwoArguments() public pure returns(uint){
      return getSum(1,2);
   }
   function callSumWithThreeArguments() public pure returns(uint){
      return getSum(1,2,3);
   }
}

使用Solidity First Application一章中提供的步骤运行上述程序。

首先单击callSumWithTwoArguments按钮,然后单击callSumWithThreeArguments按钮以查看结果。

输出

0: uint256: 3
0: uint256: 6