scala特性
特性應用 (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使用稱為“ App ”的特征,該特征用于將對象轉換為可行的程序。 此轉換使用DelayedInit完成,并且對象繼承了名為App的特征,將使用此函數。 這會將程序代碼轉換為main中繼承的方法。
Syntax:
句法:
trait App extends DelyedInit
Let's see an example to understand the topic better,
讓我們看一個例子,以更好地理解該主題,
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創建一個程序,該程序將從命令行獲取參數并打印其乘積。
object myObject extends App
{
if (args.length == 1)
{
var product = {args(0).toInt}*1
println("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 8
Here, the object with App will act as the main function and will take arguments and do the operation as required.
在這里,帶有App的對象將充當主要功能,并將接受參數并根據需要執行操作。
翻譯自: https://www.includehelp.com/scala/trait-app.aspx
scala特性