scala 方法調用_Scala中的方法調用

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編程語言調用方法的合法且正確的技術。 通常使用兩種方法來訪問類的方法。

  1. Object Creation

    對象創建

  2. 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 方法調用

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540753.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540753.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540753.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

docker lnmp php

使用 Docker 構建 LNMP 環境https://segmentfault.com/a/1190000008833012Docker 快速上手指南https://segmentfault.com/a/1190000008822648#articleHeader44

根據分類id找出父類id

2019獨角獸企業重金招聘Python工程師標準>>> 數組格式要求 id > pid $columns [ 1 > 0, 10 > 1, 200 > 10 ]; public function getP($columns,$pid) { 模擬 $pid 200; $arr $columns; while($arr[$pid]) { …

不穩定學習器適合做基分類器_分類穩定性

不穩定學習器適合做基分類器什么是分類? (What is sorting?) It means to arrange data elements in increasing or decreasing order. There are many algorithms to do so like mergesort, quicksort, counting sort etc but there are pros and cons of each al…

JavaScript基礎學習--05自定義屬性、索引值

Demos&#xff1a; https://github.com/jiangheyan/JavaScriptBase 一、自定義屬性1、讀寫操作<input abc"123" type"button" value"按鈕" />//讀寫&#xff1a; var aBtn document.getElementsByTagName(input); aBtn[0].abc 456; 2、…

有線電視pcr是什么意思_有線電視的完整形式是什么?

有線電視pcr是什么意思有線電視&#xff1a;社區訪問電視 (CATV: Community Access Television) CATV is an abbreviation of "Community Access Television". CATV是“社區訪問電視”的縮寫 。 It is also known as Public Access Television, which is a type of …

桶分類 算法_桶分類算法

桶分類 算法桶分類 (Bucket Sort) Bucket sort is a sorting technique in which array is partitioned into the buckets. By this, each bucket will be sorted individually, by using some another sorting algorithm such as insertion sort. This sorting technique assu…

百度之星初賽(A)——T5

今夕何夕 Problem Description今天是2017年8月6日&#xff0c;農歷閏六月十五。 小度獨自憑欄&#xff0c;望著一輪圓月&#xff0c;發出了“今夕何夕&#xff0c;見此良人”的寂寞感慨。 為了排遣郁結&#xff0c;它決定思考一個數學問題&#xff1a;接下來最近的哪一年里的同…

mycat 不得不說的緣分

1&#xff0c;愕然回首。它在燈火闌珊處關于mysql集群中間件。曾經寫在應用程序里面&#xff0c;由開發者實現&#xff0c;在配置文件中面寫多個數據源&#xff0c;寫庫一個數據源&#xff0c;讀庫一個數據源&#xff0c;笨拙不高效&#xff0c;由于程序猿的差異化。效果并非特…

python創建空元組_用Python創建空元組

python創建空元組Python | 空元組 (Python | empty tuple) In python, we can also create a tuple without having any element. An empty tuple is created using a pair of round brackets, (). 在python中&#xff0c;我們也可以創建一個沒有任何元素的元組 。 使用一對圓括…

共享馬扎的火爆,原來是一場營銷!

如今&#xff0c;人們的生活仿佛已經被“共享化”&#xff1a;上班有共享單車、睡覺有共享床鋪、商場有共享充電寶、去機場有共享巴士……好像除了男女朋友是自己的&#xff0c;其他都要共享了&#xff01;哎&#xff0c;不對&#xff01;前些日子&#xff0c;竟然還真有了共享…

什么是CDP(連續數據保護)?

CDP&#xff1a;連續數據保護 (CDP: Continuous Data Protection) CDP is an abbreviation of "Continuous Data Protection". It is also called as a real-time backup, is a system of data storage that backs up data in an organization or enterprise on a sy…

Git實戰(二)原理

上次的博文Git實戰&#xff08;一&#xff09;版本號控制概述中我們簡介了一下版本號控制系統的概念&#xff0c;重點對版本號控制的三種類型進行了分析和對照&#xff0c;從本篇博文開始我們進入Git的世界&#xff0c;首先介紹一下Git實現版本號控制的原理。 Git與SVN等其它版…

什么是html的混雜模式_HTML的完整形式是什么?

什么是html的混雜模式HTML&#xff1a;超文本標記語言 (HTML: Hyper Text Markup Language) HTML is an abbreviation of Hypertext markup language. Hypertext markup language is a text based standard markup language used to create web pages and design documents whi…

PHP-Manual的學習----【語言參考】----【類型】-----【對象】

Object 對象1.對象初始化要創建一個新的對象 object &#xff0c;使用 new 語句實例化一個類&#xff1a; class foo{ function do_foo(){ echo "1111"; }}$bar new foo;echo $bar->do_foo();輸出&#xff1a;1111注解&#xff1a;一個類可以初始化…

kotlin 兩個數字相加_Kotlin程序交換兩個數字

kotlin 兩個數字相加Given two numbers, we have to swap them. 給定兩個數字&#xff0c;我們必須交換它們。 Example: 例&#xff1a; Input:First number: 10Second number: 20Output:First number: 20Second number: 10To swap two numbers – here we are using third v…

Kotlin入門(14)繼承的那些事兒

上一篇文章介紹了類對成員的聲明方式與使用過程&#xff0c;從而初步了解了類的成員及其運用。不過早在《Kotlin入門(12)類的概貌與構造》中&#xff0c;提到MainActivity繼承自AppCompatActivity&#xff0c;而Kotlin對于類繼承的寫法是“class MainActivity : AppCompatActiv…

在Scala中列出| 關于Scala列表的完整教程

Scala | 清單 (Scala | List) List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable collection which means the elements of a list cannot be altered after the list is created. Lists are flexible with data types, …

MySQL單機多實例部署詳解之------多實例分別定義不同的配置文件

mysql多實例安裝---分別定義不同的配置文件1.安裝MySQL需要的依賴包[rootMySQL ~]# yum install ncurses-devel libaio-devel cmake -y[rootMySQL ~]# rpm -qa ncurses-devel libaio-develncurses-devel-5.7-4.20090207.el6.x86_64libaio-devel-0.3.107-10.el6.x86_642.安裝編譯…

javascript模塊_JavaScript中的模塊

javascript模塊JavaScript模塊 (JavaScript Modules) One of the key features of programming fundamentals is breaking down your code into fragments. These fragments depending on its functionality have been coined various terms like functions, components, modul…

在mac上安裝Docker

1.進入一下地址進行下載docker https://download.docker.com/mac/stable/Docker.dmg 進入后進行下載后進行安裝 2.將其拖動到Appliaction中即可 3.第一打開會有一個這樣的歡迎頁面 3.檢查是否安裝完成 出現上圖所示標示安裝完成了