App in Scala
Created by Wang, Jerry on Sep 25, 2015
application里一個(gè)object extend了一個(gè)App:
通過(guò)查看source code的實(shí)現(xiàn)能發(fā)現(xiàn)App是一個(gè)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 特質(zhì)的類(lèi),其初始化方法都會(huì)被挪到delayedInit方法中。App特質(zhì)的main方法捕獲到命令行參數(shù),調(diào)用delayedInit方法。
要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的App in Scala的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦幻西游手游哪个门派厉害
- 下一篇: Scala学习笔记:how to con