App in Scala
Created by Wang, Jerry on Sep 25, 2015
application里一個object extend了一個App:
通過查看source code的實現能發現App是一個trait,繼承了DelayedInit:
scala.DelayedInit
Classes and objects (but note, not traits) inheriting the DelayedInit marker trait will have their initialization code rewritten as follows: code becomes delayedInit(code).
Initialization code comprises all statements and all value definitions that are executed during initialization.
Example:
trait Helper extends DelayedInit { def delayedInit(body: => Unit) = { println(“dummy text, printed before initialization of C”) body // evaluates the initialization code of C } } class C extends Helper { println(“this is the initialization code of C”) } object Test extends App { val c = new C }
Should result in the following being printed:
dummy text, printed before initialization of C this is the initialization code of C
所有帶有App 特質的類,其初始化方法都會被挪到delayedInit方法中。App特質的main方法捕獲到命令行參數,調用delayedInit方法。
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的App in Scala的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦幻西游手游哪个门派厉害
- 下一篇: Scala学习笔记:how to con