在Scala中使用App trait和main方法的区别 [英] Difference between using App trait and main method in scala

查看:44
本文介绍了在Scala中使用App trait和main方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

object Application extends App {
   println("Hello World")
}

object Application {
    def main(args: Array[String]): Unit = {
        println("Hello World");
    }
}

推荐答案

App trait 是一种创建可执行 Scala 程序的便捷方式.与 main 方法替代的不同之处在于(除了明显的语法差异)App trait 使用了延迟初始化功能.

The App trait is a convenient way of creating an executable scala program. The difference to the main method altenative is (apart from the obvious syntactic differences) that the App trait uses the delayed initalization feature.

来自 2.9 的发行说明(参见 http://www.scala-lang.组织/旧/节点/9483 )

From the release notes for 2.9 (see http://www.scala-lang.org/old/node/9483 )

继承 App trait 的对象使用 Scala 2.9 的延迟初始化特性来执行整个主体,作为继承的 main 方法的一部分.

Objects inheriting the App trait instead make use of Scala 2.9’s delayed initialization feature to execute the whole body as part of an inherited main method.

App 方案的另一个新特性是现在可以通过 args 值(继承自 trait App)访问命令行参数

Another new feature of the App scheme is that command line arguments are now accessible via the args value (which is inherited from trait App)

这篇关于在Scala中使用App trait和main方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆