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 方法重載