scala中def_def關鍵字以及Scala中的示例

scala中def

Scala def關鍵字 (Scala def keyword)

The def keyword in Scala is used to declare functions and methods in Scala. Scala being ignorant on the data types does the same with the return type of a function. Declaring and defining a function in Scala does not strictly require a return type. The def keyword usage makes the Scala program more flexible.

Scala中def關鍵字用于在Scala中聲明函數和方法。 對數據類型無知的Scala與函數的返回類型相同。 在Scala中聲明和定義函數并不嚴格要求返回類型。 def關鍵字的使用使Scala程序更加靈活。

The function or methods that are defined using Scala def keyword get evaluated when they are called. This practice reduces the load on compiler because if a function is not called in some case. It is not evaluated.

使用Scala def關鍵字定義的函數或方法在被調用時會得到評估。 這種做法減少了編譯器的負擔,因為如果在某些情況下未調用函數。 不評估。

A function is said to be an anonymous function ( without a name ) if it is declared without using the def keyword and cannot be referenced. So, the functions with def keyword are used when the function call is required. And giving a name to it is important and using def keyword allows it.

如果某個函數未使用def關鍵字進行聲明且無法引用,則稱該函數為匿名函數(無名稱)。 因此,當需要調用函數時,將使用帶有def關鍵字的函數。 給它起一個名字很重要,并且使用def關鍵字允許它。

Syntax (declaration):

語法(聲明):

    def function_name(arguments ) : returntype;

Definition:

定義:

    def function_name(arguments) : returntype {
//code to be executed...
}

Syntax explanation:

語法說明:

Here, Scala def keyword is used to define the function, the set of arguments of the function are enclosed in the brackets and an optional return type can also be given.

在這里,使用Scala def關鍵字定義函數,函數的參數集放在方括號中,還可以提供可選的返回類型。

Example code:

示例代碼:

object MyClass {
def add(x:Int, y:Int) : Int = {
var sum = x+y ; 
return sum; 
}
def main(args: Array[String]) {
print("sum of x + y = " + add(25,10));
}
}

Output

輸出量

sum of x + y = 35

Code explanation:

代碼說明:

The above code prints the sum of two numbers using a function. The function add is used to add two numbers and returns their result. The function used Int return type to return the sum of two numbers passes as arguments of the function. The returned value is printed using the print function in the main class.

上面的代碼使用一個函數打印兩個數字的和。 函數add用于將兩個數字相加并返回其結果。 該函數使用Int返回類型返回兩個數字的和,作為函數的參數傳遞。 返回的值使用主類中的打印功能進行打印。

翻譯自: https://www.includehelp.com/scala/def-keyword-with-example-in-scala.aspx

scala中def

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

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

相關文章

前端必備的 web 安全知識手記

前言 安全這種東西就是不發生則已,一發生則驚人。作為前端,平時對這方面的知識沒啥研究,最近了解了下,特此沉淀。文章內容包括以下幾個典型的 web 安全知識點:XSS、CSRF、點擊劫持、SQL 注入和上傳問題等(…

php腳本開頭注釋_PHP文件注釋標記及規范小結

PHP文件注釋標記及規范小結發布時間:2016-06-17 來源: 點擊:次PHP 注釋標記access使用范圍:class,function,var,define,module該標記用于指明關鍵字的存取權限:private、public或protecedauthor指明作者copyright使用范圍&#xf…

Salesforce宣布5.82億美元收購文件編輯公司Quip

北京時間8月2日消息,據路透社報道,云軟件開發商Salesforce.com周一宣布,已同意以大約5.82億美元收購文件編輯創業公司Quip。 Salesforce此前已經對Quip進行了投資。Quip開發的文字處理平臺供企業員工用于在移動設備、可穿戴設備以及臺式機上編…

Java中的main()方法是強制性的嗎?

The question is that "Is main() method is compulsory in Java?" 問題是“ main()方法在Java中是強制性的嗎?” Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5. 是的…

php date( w ),PHP Date()函數詳解

頁面的最上方加上:date_default_timezone_set(PRC); /*把時間調到北京時間,php5默認為格林威治標準時間*/date ()a: "am"或是"pm"A: "AM"或是"PM"d: 幾日,兩位數字,若不足則補零&#xff1b…

10.6-全棧Java筆記:常見流詳解(四)

上節我們講到「Java中常用流:數據流和對象流」,本節我們學習文件字符流和文件緩沖流~文件字符流前面介紹的文件字節流可以處理所有的文件,但是字節流不能很好的處理Unicode字符,經常會出現“亂碼”現象。所以,我們處理…

python 示例_帶有示例的Python File open()方法

python 示例文件open()方法 (File open() Method) open() method is an inbuilt method in Python, it is used to create, open or append a file. open()方法是Python中的內置方法,用于創建,打開或附加文件。 Syntax: 句法: file_object …

php屬于腳本,php是腳本語言嗎

PHP即“超文本預處理器”,是一種通用開源腳本語言。PHP是在服務器端執行的腳本語言,與C語言類似,是常用的網站編程語言。PHP獨特的語法混合了C、Java、Perl以及 PHP 自創的語法。利于學習,使用廣泛,主要適用于Web開發領…

NetMarketShare:本月桌面瀏覽器市場份額幾乎沒有變化

NetMarketShare之前關于臺式機瀏覽器市場份額的報告表示,Google Chrome市場份額正在快速上升,而Edge瀏覽器市場份額正在以蝸牛的速度前進。而該公司的最新統計數據顯示,幾乎所有瀏覽器的市場份額或多或少保持不變。 NetMarketShare的最新統計…

treeset java_Java TreeSet add()方法與示例

treeset javaTreeSet類的add()方法 (TreeSet Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given object(ob) to this TreeSet when it does not already exist otherwise it ignore…

php fpm www.conf,PHP7中php.ini、php-fpm和www.conf 配置

PHP7中php.ini、php-fpm和www.conf 配置php.ini是php運行核心配置文件,下面是一些常用配置extension_dir""● 設置PHP的擴展庫路徑expose_php Off● 避免PHP信息暴露在http頭中display_errors Off● 避免暴露php調用mysql的錯誤信息log_errors On● 在關閉display…

服務器電流源泉ups電源的三大形式

還記得此前12306官網癱瘓,回家心切急于購票的我們只能感到無比心累。雙十一前夕,守在購物車邊準備瘋狂購物的剁手黨們,遇到一直呈現加載狀態的頁面,不得不感嘆想要做馬云背后的那個人也要大費周折。作為一個資深網民,不…

timer purge_Java Timer purge()方法與示例

timer purge計時器類purge()方法 (Timer Class purge() method) purge() method is available in java.util package. purge()方法在java.util包中可用。 purge() method is used to remove all canceled tasks from the task queue of this Timer. purge()方法用于從此Timer的…

大話Linux內核中鎖機制之原子操作、自旋鎖【轉】

轉自:http://blog.sina.com.cn/s/blog_6d7fa49b01014q7p.html 多人會問這樣的問題,Linux內核中提供了各式各樣的同步鎖機制到底有何作用?追根到底其實是由于操作系統中存在多進程對共享資源的并發訪問,從而引起了進程間的競態。這…

hashmap put方法_Java HashMap put()方法與示例

hashmap put方法HashMap類的put()方法 (HashMap Class put() method) put() method is available in java.util package. put()方法在java.util包中可用。 put() method is used to link the given value element with the given key element in this HashMap. put()方法用于在…

java中jdom,java – JDOM中的命名空間(默認)

我正在嘗試使用最新的JDOM包生成XML文檔.我遇到了根元素和命名空間的問題.我需要生成這個根元素:xmlns"http://www.energystar.gov/manageBldgs/req"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://www.…

java enummap_Java EnumMap get()方法與示例

java enummapEnumMap類的get()方法 (EnumMap Class get() method) get() method is available in java.util package. get()方法在java.util包中可用。 get() method is used to get the value mapped with the given key element (key_ele) otherwise it returns null when no…

java后臺json傳遞,后臺json傳遞

json除了可以用于前臺傳遞,還可用于后臺之間傳遞。它可以傳遞List,Map,Bean等類型的數據。例如:User u1new User();u1.setUsername("zy");u1.setPassword("123");User u2new User();u2.setUsername("msl");u2.setPassword…

專家呼吁建安全漏洞信息共享機制并強化管控

近日,由中國網絡空間安全協會主辦,中國網絡空間安全協會網絡空間安全法律與公共政策專業委員會、北京郵電大學互聯網治理與法律研究中心、公安部第三研究所網絡安全法律研究中心、西安交通大學信息安全法律研究中心承辦的《網絡安全法(草案二…

java的equals方法_Java Date equals()方法與示例

java的equals方法日期類equals()方法 (Date Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this date and the given object (o) are equals or not. equals()方法…