Scala中的方法重载用法指南

2021年3月17日14:29:04 发表评论 944 次浏览

方法重载是常见的实现方式多态性。它是一种以多种形式重新定义功能的能力。用户可以通过在共享相同名称的类中定义两个或多个函数来实现函数重载。 Scala可以区分具有不同方法签名的方法。即, 方法可以在同一类中具有相同的名称, 但具有不同的参数列表(即, 参数的数量, 参数的顺序和参数的数据类型)。

  • 重载方法根据作为参数传递给方法的参数的数量和类型进行区分。
  • 我们不能使用相同的名称, 顺序和参数的类型定义多个方法。这将是编译器错误。
  • 区分重载方法时, 编译器不考虑返回类型。但是你不能声明具有相同签名和不同返回类型的两个方法。它将引发编译时错误。

为什么我们需要方法重载?

如果我们需要以不同的方式(即针对不同的输入)进行相同类型的操作。在下面描述的示例中, 我们对不同的输入进行加法运算。很难为单个动作找到许多不同的有意义的名称。

进行重载方法的不同方法

方法重载可以通过更改来完成:

  • 两种方法中的参数数量。
  • 方法的参数的数据类型。
  • 方法的参数顺序。

通过更改参数数量。

例子:

// Scala program to demonstrate the function 
// overloading by changing the number 
// of parameters 
class GFG
{
      
     // function 1 with two parameters
     def fun(p : Int, q : Int)
     {
         var Sum = p + q;
         println( "Sum in function 1 is:" + Sum);
     }
      
     // function 2 with three parameters
     def fun(p : Int, q : Int, r : Int)
     {
         var Sum = p + q + r;
         println( "Sum in function 2 is:" + Sum);
     }
}
object Main 
{
     // Main function
     def main(args : Array[String]) 
     {
          
     // Creating object of GFG class
     var obj = new GFG();
     obj.fun( 6 , 8 );
     obj.fun( 5 , 10 , 58 );
     }
}

输出如下:

Sum in function 1 is:14
Sum in function 2 is:73

通过更改参数的数据类型

例子:

// Scala program to demonstrate the function 
// overloading by changing the data types 
// of the parameters 
class GFG
{
      
     // Adding three integer elements
     def fun(p : Int, q : Int, r : Int)
     {
         var Sum = p + q + r;
         println( "Sum in function 1 is:" +Sum);
     }
      
     // Adding three double elements
     def fun(p : Double, q : Double, r : Double)
     {
         var Sum = p + q + r;
         println( "Sum in function 2 is:" +Sum);
     }
}
object Main 
{
     // Main method
     def main(args : Array[String]) 
     {
          
     // Creating object of GFG class
     var obj = new GFG();
     obj.fun( 6 , 8 , 10 );
     obj.fun( 5.9 , 10.01 , 58.7 );
     }
}

输出如下:

Sum in function 1 is:24
Sum in function 2 is:74.61

通过更改参数的顺序

例子:

// Scala program to demonstrate the function 
// overloading by changing the 
// order of the parameters 
class GFG
{
      
     // Function 1
     def fun(name : String, No : Int)
     {
         println( "Name of the watch company is:" + name);
         println( "Total number of watch :" + No);
     }
      
     // Function 2
     def fun(No : Int, name : String )
     {
         println( "Name of the watch company is:" + name);
         println( "Total number of watch :" + No);
     }
}
object Main 
{
     // Main Function
     def main(args : Array[String])
     {
          
     // Creating object of GFG class
     var obj = new GFG();
     obj.fun( "Rolex" , 10 );
     obj.fun( "Omega" , 10 );
     }
}

输出如下:

Name of the watch company is:Rolex
Total number of watch :10
Name of the watch company is:Omega
Total number of watch :10

当方法签名相同且返回类型不同时会发生什么?

编译器将给出错误, 因为仅返回值不足以使编译器找出必须调用的函数。仅当两个方法都具有不同的参数类型(因此, 它们具有不同的签名)时, 方法重载才是可能的。

例子:

// Example to show error when method signature is same  
// and return type is different. 
object Main {
           
         // Main method
         def main(args : Array[String]) {
      println( "Sum in function 1 is:" + fun( 6 , 8 ) );
      println( "Sum in function 2 is:" + fun( 6 , 8 ) );
     }
      
      // function 1 
     def fun(p : Int, q : Int) : Int = {
         var Sum : Int = p + q;
         return Sum;
         
     }
      
  // function 2 
     def fun(p : Int, q : Int) : Double = {
         var Sum : Double = p + q + 3.7 ;
         return Sum;
     }
     
      
  
}

编译时错误:

prog.scala:10:错误:对重载定义的模棱两可, 这两种方法都在对象Main类型的主方法中(p:Int, q:Int)Double和方法在对象Main类型的主方法中(p:Int, q:Int)匹配参数类型(Int, Int)和预期结果类型Any println("函数1的总和为:" + fun(6, 8)); ^ prog.scala:11:错误:对重载定义的含糊不清的引用, 两个方法都在对象Main类型的主方法中(p:Int, q:Int)Double和方法在对象Main类型的主方法中(p:Int, q:Int) Int匹配参数类型(Int, Int)和预期结果类型Any println("函数2的总和为:" + fun(6, 8)); ^ prog.scala:22:错误:方法fun被定义了两次冲突的符号, 都源自文件" prog.scala" def fun(p:Int, q:Int):Double = {^找到了三个错误


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: