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