前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >App in Scala

App in Scala

作者头像
Jerry Wang
发布2020-03-06 14:30:16
5910
发布2020-03-06 14:30:16
举报

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方法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Created by Wang, Jerry on Sep 25, 2015
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档