scala重载无参构造方法_Scala中的无参数方法
scala重載無參構造方法
Scala無參數方法 (Scala parameterless method)
A method which accepts no parameters from the calling code. It also denotes that there will not be any empty parentheses. These are special types of methods in Scala that are initialized and called especially. To initialize parameterless method you need to use the name with def keyword but w/o the "( )" parenthesis. While calling the method also we will avoid using the?"( )" parenthesis.
一種不從調用代碼接受任何參數的方法。 它還表示不會有任何空括號。 這些是Scala中特殊的方法類型,這些方法特別進行了初始化和調用。 要初始化無參數方法,您需要使用帶def關鍵字的名稱,但不帶“()”括號。 在調用該方法時,我們也將避免使用“()”括號。
Syntax:
句法:
def method_name = code_to_be_executedProgram to illustrate parameterless method
說明無參數方法的程序
class calculator(a: Int, b: Int) { def add = println("a+b = " + (a+b)) def subtract = println("a-b = " + (a-b)) def product = println("a*b = "+ (a*b) )def division = println("a/b = " + (a/b))} object Main { def main(args: Array[String]) { val calc = new calculator(1250, 50) println("value of a = 1250 and b = 50")calc.addcalc.subtractcalc.productcalc.division} }Output
輸出量
value of a = 1250 and b = 50 a+b = 1300 a-b = 1200 a*b = 62500 a/b = 25Explanation:
說明:
This code initializes a class calculator and defines 4 parameterless methods in it. These methods are used to add, subtract, multiply and divide values. These methods directly print the calculated values and do not accept any parameters. The object calc of the class calculator is created with values 1250 and 50. Then with this calc object, we will call all the four methods that perform operations and returns output.
此代碼初始化一個類計算器,并在其中定義4個無參數的方法 。 這些方法用于加,減,乘和除值。 這些方法直接打印計算值,不接受任何參數。 將使用值1250和50創建類計算器的對象calc 。 然后,使用該calc對象,我們將調用執行操作并返回輸出的所有四個方法。
The parameterless method is?invoked?without the ()?parenthesis.?And there is an error if we call a parameterless method using the parenthesis.
無參方法在不帶()括號的情況下調用。 如果我們使用括號調用無參數方法 ,則會出現錯誤。
If the programmer by mistake puts a parenthesis at the end of the function call, the compiler would report an error stating: "Unit does not take parameters".
如果程序員錯誤地在函數調用的末尾加上了括號,則編譯器將報告錯誤,指出:“單元不帶參數”。
Program to show error when parameterless method is called using parentheses
程序在使用括號調用無參數方法時顯示錯誤
class calculator(a: Int, b: Int) { def division = println("a/b = " + (a/b))} object Main { def main(args: Array[String]) { val calc = new calculator(1250, 50) println("value of a = 1250 and b = 50")calc.division()} }Output
輸出量
/home/jdoodle.scala:12: error: Unit does not take parameterscalc.division()^ one error foundExplanation:
說明:
This code has the same logic as the?former code.?The difference is just that while calling the division parameterless method parenthesis is used.?Due to this, the compiler gives an error: "Unit does not take parameters".
此代碼與以前的代碼具有相同的邏輯。 區別只是在調用除法無參數方法時使用了括號。 因此,編譯器將給出錯誤:“單元不帶參數”。
Bonus : parameterless method Vs method without parameter
獎勵:無參數方法與無參數方法
Scala has supports multiple types of parameter passing in its methods.
Scala在其方法中支持多種類型的參數傳遞。
But these two methods look quite similar but have different functionalities. The parameterless method does not accept and sort of parameter in its call. But the method without parameter accepts void parameter in its call ( nothing can be treated as a parameter).
但是這兩種方法看起來很相似,但是功能不同。 無參數方法在其調用中不接受參數并對其進行排序。 但是不帶參數的方法在其調用中接受void參數(不能將任何內容視為參數)。
翻譯自: https://www.includehelp.com/scala/parameterless-method-in-scala.aspx
scala重載無參構造方法
總結
以上是生活随笔為你收集整理的scala重载无参构造方法_Scala中的无参数方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby array_Ruby中带有示例
- 下一篇: java reverse_Java In