日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scala 方法调用_Scala中的方法调用

發(fā)布時(shí)間:2025/3/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scala 方法调用_Scala中的方法调用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

scala 方法調(diào)用

Scala方法調(diào)用 (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.

方法調(diào)用是用Scala編程語言調(diào)用方法的合法且正確的技術(shù)。 通常使用兩種方法來訪問類的方法。

  • Object Creation

    對象創(chuàng)建

  • Inheritance

    遺產(chǎn)

  • We dynamically call methods of a class using the object of the class. the correct way of accessing the methods using object are:

    我們使用類的對象動態(tài)調(diào)用類的方法。 使用object訪問方法的正確方法是:

    1)使用點(diǎn)運(yùn)算符調(diào)用方法 (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,

    從類的對象調(diào)用類的方法時(shí),我們使用點(diǎn)(。)運(yùn)算符。 使用點(diǎn)運(yùn)算符訪問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,

    這是使用對象訪問類的方法的最合適的方法。 除此以外,可以使用某些方法,但并不理想。 這些方法不會產(chǎn)生任何錯(cuò)誤,但不是正確的方法。 他們是,

    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)直接調(diào)用方法 (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,

    從類中調(diào)用方法將繼承基類或同一類。 調(diào)用該方法的正確合法方法是:

    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)調(diào)用帶有參數(shù)的方法 (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.

    當(dāng)方法包含參數(shù)并使用參數(shù)調(diào)用方法時(shí)。 對于使用參數(shù)調(diào)用方法,我們將使用逗號和單個(gè)空格分隔參數(shù)。

    object_name.method_name(parameter1, parameter2)

    Other legal methods that are not correct but does not compile to an error,

    其他不正確但無法編譯為錯(cuò)誤的合法方法,

    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)調(diào)用特殊方法 (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.

    當(dāng)Scala中的方法不接受任何參數(shù)時(shí)。 因此,在調(diào)用時(shí),不會傳遞使括號成為可選參數(shù)的參數(shù)。 沒有括號,代碼變得更具可讀性,并且使程序員易于工作。

    Both this is legal and correct ways to invoke a method that does not take any parameter.

    這是調(diào)用不帶任何參數(shù)的方法的合法方法和正確方法。

    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 方法調(diào)用

    總結(jié)

    以上是生活随笔為你收集整理的scala 方法调用_Scala中的方法调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。