Solidity - 函数重载
同一作用域内的同一函数名可以有多个定义。 函数的定义必须在参数列表中的参数类型和/或数量上有所不同。 您不能重载仅返回类型不同的函数声明。
以下示例展示了 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 应用 章节中提供的步骤运行上述程序。
先单击 callSumWithTwoArguments 按钮,然后单击 callSumWithThreeArguments 按钮查看结果。
输出
0: uint256: 3 0: uint256: 6

