1.App类的代码
延迟初始化,就是一开始的时候只是造了个对象出来,但是类的属性并没有填充上,当你需要用的时候,才会填充上,才真正完成实例化
DelayedInit也是实现了类似的功能
App类的注释非常精彩:
/** The `App` trait can be used to quickly turn objects
* into executable programs. Here is an example:
* {{{
* object Main extends App {
* Console.println("Hello World: " + (args mkString ", "))
* }
* }}}
* Here, object `Main` inherits the `main` method of `App`.
*
* `args` returns the current command line arguments as an array.
*
* ==Caveats==
*
* '''''It should be noted that this trait is implemented using the [[DelayedInit]]
* functionality, which means that fields of the object will not have been initialized
* before the main method has been executed.'''''
*
* It should also be noted that the `main` method will not normally need to be overridden:
* the purpose is to turn the whole class body into the “main method”. You should only
* chose to override it if you know what you are doing.
*
* @author Martin Odersky
* @version 2.1, 15/02/2011
*/
trait App extends DelayedInit {
protected def args: Array[String] = _args
private var _args: Array[String] = _
private val initCode = new ListBuffer[() => Unit]
private val initCode = new ListBuffer[() => Unit]
//因为所有的main方法返回值的都是Unit,所以这里返回值是Unit
/** The init hook. This saves all initialization code for execution within `main`.
* This method is normally never called directly from user code.
* Instead it is called as compiler-generated code for those classes and objects
* (but not traits) that inherit from the `DelayedInit` trait and that do not
* themselves define a `delayedInit` method.
* @param body the initialization code to be stored for later execution
*/
override def delayedInit(body: => Unit) {
initCode += (() => body)
}
/** The main method.
* This stores all argument so that they can be retrieved with `args`
* and the executes all initialization code segments in the order they were
* passed to `delayedInit`
* @param args the arguments passed to the main method
*/
def main(args: Array[String]) = {
this._args = args
for (proc <- initCode) proc()
if (util.Properties.propIsSet("scala.time")) {
val total = currentTime - executionStart
Console.println("[total " + total + "ms]")
}
}
那在哪里能看到把body代码加进去initCode的呢?
可以看到接口:
/** 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
* }}}
*
* @see "Delayed Initialization" subsection of the Scala Language Specification (section 5.1)
*
* @author Martin Odersky
*/
trait DelayedInit {
def delayedInit(x: => Unit): Unit
}
注释非常精彩,假如改成:
val c=new C()
println("end...")
end...在最后才打印出来,所以delayedInit究竟是怎么传进去的?什么时候传进去的?
看一个例子:
trait Helper extends DelayedInit {
override def delayedInit(body: => Unit): Unit = {
println("dummy text, printed before initialization of C")
body
}
}
class C extends Helper{
println("this is the initialization code of C")
}
object AppInternals extends App {
val c=new C()
println("end...")
}
反编译C$delayedInit$body.class:
![)8_]{U(FT))YP810K3)UA0N.png](http://upload-images.jianshu.io/upload_images/2735954-691b5d5df5efdfe6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其实是这样的:在编译的时候,会把代码赋值给body,
然后在执行的时候,main方法会调用delayInit(),把body传进去,才真正地进行初始化
延迟初始化是不是适用于任何情况呢?
肯定不是的,假如一个类要依赖于另外一个类的业务逻辑的时候,这时候一般就不行了
归纳总结:1.关于延迟初始化
2.关于App类
3.看例子以及理解背后的原理
4.延迟初始化适用于所有情况吗?