scala特性_Scala | 特性应用

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特性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值