scala 方法重载
Scala方法重载 (Scala method overloading)
Method overloading is a method that is redefined in different ways under the same name. Method overloading is one of the methods used to implement polymorphism in Scala.
方法重载是一种使用相同名称以不同方式重新定义的方法。 方法重载是用于在Scala中实现多态的方法之一。
Implementation of method overloading in Scala
Scala中方法重载的实现
To create an overloaded method in Scala, define multiple methods with the same name and different parameter list and return type. This means defining multiple functions one with one parameter of type ‘A’ other with two parameters, etc.
要在Scala中创建重载方法 ,请定义多个具有相同名称,不同参数列表和返回类型的方法。 这意味着定义多个功能,一个具有一个“ A”类型的参数,另一个具有两个参数,等等。
Syntax:
句法:
//Method 1 :
def fun (a : data_type ) {
// code to be executed
}
//Method 2 :
def fun(a : data_type , b : data_type ){
// code to be executed
}
Both these methods contribute to method overloading in Scala. Now, to create an overloaded method can invert either of the following things in the function that lead to overloading the method:
这两种方法都会导致Scala中的方法重载。 现在,要创建一个重载的方法,可以在函数中反转以下任何事情,从而导致该方法的重载 :
Methods with different parameter lists
具有不同参数列表的方法
Methods with different data types and order
具有不同数据类型和顺序的方法
Program to show implementation of method overloading in Scala | print area of different figures using method overloading
程序展示Scala中方法重载的实现 使用方法重载来打印不同图形的区域
object MyClass {
def peri(x:Int, y:Int){
println("The perimeter of rectangle is "+ (x+y))
}
def peri(a:Int , b:Int ,c:Int){
println("The perimeter of rectangle is "+ (a+b+c))
}
def peri(r:Int){
println("The perimeter of rectangle is "+ (2*(3.14)*r))
}
def main(args: Array[String]) {
println("Program to print perimeter of different figures using method overloading: ")
println("Perimeter of rectangle: ")
peri(12 , 345)
println("Perimeter of triangle: ")
peri(3, 5, 8)
println("Perimeter of circle:")
peri(4)
}
}
Output
输出量
Program to print perimeter of different figures using method overloading:
Perimeter of rectangle:
The perimeter of rectangle is 357
Perimeter of triangle:
The perimeter of rectangle is 16
Perimeter of circle:
The perimeter of rectangle is 25.12
Explanation:
说明:
The above code is used to print demonstrate method overloading concept. This example is to print the perimeter of different figures using object overloading. The function peri() is overloaded and each of the different figure areas will have a different overloaded method that will be able to calculate the given area of the specific figure. The code prints the calculated value for the figure.
上面的代码用于打印演示方法重载的概念。 本示例将使用对象重载来打印不同图形的周长。 函数peri()已重载,每个不同的图形区域将具有不同的重载方法,该方法将能够计算特定图形的给定区域。 该代码将打印图形的计算值。
Program to show implementation of method overloading in Scala | print data-type of the parameter using method overloading
程序展示Scala中方法重载的实现 使用方法重载打印参数的数据类型
object MyClass {
def datatype(x:Int){
println("The parameter is of Integer datatype")
}
def datatype(x:Float){
println("The parameter is of Float data type")
}
def datatype(x:Char){
println("The parameter is of Character data type")
}
def datatype(x: Boolean){
println("The parameter is of Boolean data type")
}
def main(args: Array[String]) {
println("Program to print data type using method overloading: ")
datatype(4)
datatype(4.0f)
datatype('f')
datatype(true)
}
}
Output
输出量
Program to print data type using method overloading:
The parameter is of Integer datatype
The parameter is of Float data type
The parameter is of Character data type
The parameter is of Boolean data type
Explanation:
说明:
The above code is used to print the data type of the variable. The method datatype() is overloaded to print the data type based on the input variable. The method can check for Int, Float, Char, Bool data type and will check for the data type, and prints the data type accordingly.
上面的代码用于打印变量的数据类型。 方法datatype()重载以根据输入变量输出数据类型。 该方法可以检查Int , Float , Char , Bool数据类型,并将检查该数据类型,并相应地打印该数据类型。
翻译自: https://www.includehelp.com/scala/method-overloading-in-scala.aspx
scala 方法重载