scala 方法重载_Scala中的方法重载
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中實現(xiàn)多態(tài)的方法之一。
Implementation of method overloading in Scala
Scala中方法重載的實現(xiàn)
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中創(chuàng)建重載方法 ,請定義多個具有相同名稱,不同參數(shù)列表和返回類型的方法。 這意味著定義多個功能,一個具有一個“ A”類型的參數(shù),另一個具有兩個參數(shù),等等。
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:
這兩種方法都會導(dǎo)致Scala中的方法重載。 現(xiàn)在,要創(chuàng)建一個重載的方法,可以在函數(shù)中反轉(zhuǎn)以下任何事情,從而導(dǎo)致該方法的重載 :
Methods with different parameter lists
具有不同參數(shù)列表的方法
Methods with different data types and order
具有不同數(shù)據(jù)類型和順序的方法
Program to show implementation of method overloading in Scala | print area of different figures using method overloading
程序展示Scala中方法重載的實現(xiàn) 使用方法重載來打印不同圖形的區(qū)域
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.12Explanation:
說明:
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.
上面的代碼用于打印演示方法重載的概念。 本示例將使用對象重載來打印不同圖形的周長。 函數(shù)peri()已重載,每個不同的圖形區(qū)域?qū)⒕哂胁煌闹剌d方法,該方法將能夠計算特定圖形的給定區(qū)域。 該代碼將打印圖形的計算值。
Program to show implementation of method overloading in Scala | print data-type of the parameter using method overloading
程序展示Scala中方法重載的實現(xiàn) 使用方法重載打印參數(shù)的數(shù)據(jù)類型
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 typeExplanation:
說明:
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.
上面的代碼用于打印變量的數(shù)據(jù)類型。 方法datatype()重載以根據(jù)輸入變量輸出數(shù)據(jù)類型。 該方法可以檢查Int , Float , Char , Bool數(shù)據(jù)類型,并將檢查該數(shù)據(jù)類型,并相應(yīng)地打印該數(shù)據(jù)類型。
翻譯自: https://www.includehelp.com/scala/method-overloading-in-scala.aspx
scala 方法重載
總結(jié)
以上是生活随笔為你收集整理的scala 方法重载_Scala中的方法重载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中卫治疗宫颈炎最好的医院推荐
- 下一篇: MTFBWU的完整形式是什么?