Scala项目启动方式:extends App和main的区别
啟動一個Scala項目,有兩種方法:
利用App特質:
object Application extends App {println("Hello World") }使用main:
object Application {def main(args: Array[String]): Unit = {println("Hello World");} }那么,這兩種啟動項目的方式有什么不同呢?
App是scala語言內置的一個特質,使用它,則把對應object內部的整體作為scala main的一部分,有延遲啟動的特性。同時,命令行參數args也作為App特質的一部分,可以被開發者直接使用。而main函數則是scala的默認啟動方式。
其實,我們可以在scala的官方文檔中找到上述內容:
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.
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)
所以,這兩種啟動方式沒有對錯之分。不過,使用App特質時,可能導致extends App的object內部有錯誤時整個項目啟動失敗。所以,用main的方式啟動項目更加穩妥。
參考鏈接:https://blog.csdn.net/sinat_35406909/article/details/104223521xxxxxxxxxx git --version
總結
以上是生活随笔為你收集整理的Scala项目启动方式:extends App和main的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CDH6.3.2界面配置hbase-si
- 下一篇: CentOS7升级Git版本