scala 方法調用
Scala方法調用 (Scala Method Invocation)
Method invocation is the legal and correct technique to call a method in Scala programming language. The methods of a class are usually accessed by using two methods.
方法調用是用Scala編程語言調用方法的合法且正確的技術。 通常使用兩種方法來訪問類的方法。
Object Creation
對象創建
Inheritance
遺產
We dynamically call methods of a class using the object of the class. the correct way of accessing the methods using object are:
我們使用類的對象動態調用類的方法。 使用object訪問方法的正確方法是:
1)使用點運算符調用方法 (1) Invoking methods using dot operator)
While calling a method of a class from the object of the class we use the dot (.) operator. The correct and legal way of accessing the methods of a class in Scala using the dot operator is,
從類的對象調用類的方法時,我們使用點(。)運算符。 使用點運算符訪問Scala中的類方法的正確合法方法是,
object_name.method_name(parameters)
This is the most appropriate way of accessing the method of a class using an object. Else than this some ways can be used but are not ideal. These methods do not generate any error but are not correct ways to call the method. They are,
這是使用對象訪問類的方法的最合適的方法。 除此以外,可以使用某些方法,但并不理想。 這些方法不會產生任何錯誤,但不是正確的方法。 他們是,
object_name . method_name(parameters)
object_name. method_name(parameters)
object_name . method_name (parameters)
Example code:
示例代碼:
class car{
def sound(noise:String){
println(noise+" goes fast")
}
}
object MyClass {
def main(args: Array[String]) {
var newcar = new car();
newcar.sound("Lamborghini!")
newcar . sound("Honda!")
newcar .sound("Mercedes!")
newcar . sound ("BMW!")
}
}
Output
輸出量
Lamborghini! goes fast
Honda! goes fast
Mercedes! goes fast
BMW! goes fast
2)直接調用方法 (2) Invoking methods directly)
Calling a method from a class inherits the base class or in the same class. The correct and legal way to call the method is,
從類中調用方法將繼承基類或同一類。 調用該方法的正確合法方法是:
method_name(parameters)
Other than this other methods are also legal. Like,
除此之外,其他方法也是合法的。 喜歡,
method_name (parameters)
method_name( parameters )
method_name ( parameters )
Example code:
示例代碼:
object MyClass {
def sound(noise:String){
println(noise+" makes noise")
}
def main(args: Array[String]) {
sound("Lamborghini!")
sound ( "Honda!")
sound ( "Mercedes!" )
}
}
Output
輸出量
Lamborghini! makes noise
Honda! makes noise
Mercedes! makes noise
3)調用帶有參數的方法 (3) Invoking methods with parameters)
When the method contains arguments and calling methods with parameters. For invoking methods with the argument we will separate the parameters using commas and single space.
當方法包含參數并使用參數調用方法時。 對于使用參數調用方法,我們將使用逗號和單個空格分隔參數。
object_name.method_name(parameter1, parameter2)
Other legal methods that are not correct but does not compile to an error,
其他不正確但無法編譯為錯誤的合法方法,
object_name. method_name(parameter1, parameter2)
object_name.method_name (parameter1, parameter2)
object_name.method_name( parameter1 , parameter2 )
object_name. method_name ( parameter1 , parameter2 )
Example code:
示例代碼:
class calculator{
def add(a:Int , b:Int){
println("The sum of "+a+" and "+b+" is "+(a+b))
}
}
object MyClass {
def main(args: Array[String]) {
var calc = new calculator();
calc.add(12,34)
calc. add(1,434)
calc.add (2,3)
calc.add(15, 4)
calc. add ( 232,314 )
}
}
Output
輸出量
The sum of 12 and 34 is 46
The sum of 1 and 434 is 435
The sum of 2 and 3 is 5
The sum of 15 and 4 is 19
The sum of 232 and 314 is 546
4)調用特殊方法 (4) Invoking special method)
When a method in Scala does not accept any argument. So, while invoking no arguments are passed which make the parentheses optional. Without parenthesis, the code becomes much more readable and also easy the programmers work.
當Scala中的方法不接受任何參數時。 因此,在調用時,不會傳遞使括號成為可選參數的參數。 沒有括號,代碼變得更具可讀性,并且使程序員易于工作。
Both this is legal and correct ways to invoke a method that does not take any parameter.
這是調用不帶任何參數的方法的合法方法和正確方法。
object_name.method_name()
object_name.method_name
Example code:
示例代碼:
class car{
def sound(){
println("Broom Broom Brooooom!!!!!")
}
}
object MyClass {
def main(args: Array[String]) {
var newcar = new car();
newcar.sound()
newcar.sound
}
}
Output
輸出量
Broom Broom Brooooom!!!!!
Broom Broom Brooooom!!!!!
翻譯自: https://www.includehelp.com/scala/method-invocation-in-scala.aspx
scala 方法調用