scala特性_Scala | 特性应用
scala特性
特性應(yīng)用 (Trait App)
Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. This will convert the program code into a method that is inherited in main.
Scala使用稱(chēng)為“ App ”的特征,該特征用于將對(duì)象轉(zhuǎn)換為可行的程序。 此轉(zhuǎn)換使用DelayedInit完成,并且對(duì)象繼承了名為App的特征,將使用此函數(shù)。 這會(huì)將程序代碼轉(zhuǎn)換為main中繼承的方法。
Syntax:
句法:
trait App extends DelyedInitLet's see an example to understand the topic better,
讓我們看一個(gè)例子,以更好地理解該主題,
In this example, we will use the App trait to create a program that will take arguments from the command line and print the product of them.
在此示例中,我們將使用App trait創(chuàng)建一個(gè)程序,該程序?qū)拿钚蝎@取參數(shù)并打印其乘積。
object myObject extends App { if (args.length == 1) { var product = {args(0).toInt}*1println("Product is "+ product) } else if (args.length == 2) { var product = {args(0).toInt}*{args(1).toInt}println("Product is "+ product) }else{ println("Values not found.") } }Output
輸出量
Command-line: 2 4 Product is 8Here, the object with App will act as the main function and will take arguments and do the operation as required.
在這里,帶有App的對(duì)象將充當(dāng)主要功能,并將接受參數(shù)并根據(jù)需要執(zhí)行操作。
翻譯自: https://www.includehelp.com/scala/trait-app.aspx
scala特性
總結(jié)
以上是生活随笔為你收集整理的scala特性_Scala | 特性应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python中的__init__和sel
- 下一篇: MB_LEN_MAX常数,带C ++示例