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 第一個應用章節中提供的步驟執行以上程式。

首先點選 callSumWithTwoArguments 按鈕,然後點選 callSumWithThreeArguments 按鈕檢視結果。

輸出

0: uint256: 3
0: uint256: 6
廣告

© . All rights reserved.