惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
小众软件
小众软件
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
MyScale Blog
MyScale Blog

博客园 - cindy_zl

SAP SD销售与分销模块 Agent认知框架ReAct&Plan RAG系统优化 RAG检索优化 RAG架构 SAA-C02-架构师助理级-英文版-Q1 scala-数组 navicat mysql创建存储过程报错 安装完kali出现中文乱码 Spark SQL是处理结构化的数据 SPARK SQL ERROR: Detected cartesian product for INNER join between logical plans报错解决方法 scala集合三大类(seq序列,set集,map映射)——map映射 scala集合三大类(seq序列,set集,map映射)——set集合 scala集合三大类(seq序列,set集,map映射)——list序列 scala元组及拉链操作 scala的map映射 scala数组 scala方法和涵数的声明以及方法转换成涵数 scala的基础数据类型&if条件表达式&for循环
scala的lazy关键字
cindy_zl · 2019-09-18 · via 博客园 - cindy_zl

scala里面的lazy一般是一个关键字,多用于放在一个变量的前面,这样表示这个变量是一个惰性变量

package day02

/**
* 在scala中用Lazy定义的变量叫惰性变量,会实现延迟加载,即在编译的时候并没有执行,
* 惰性变量只能是不可变变量,且只有在调用惰性变量时,才会去实例化这个变量。
*/

object ScalaLazyDemo1 {

def init(): Unit = {
println("call init()...")
}

def main(args: Array[String]): Unit = {
val property = init() //没有用Lazy修饰
println("after init()")
println(property)
}

}

//执行结果
/*
call init()...
after init()
()
*/

object ScalaLazyDemo2 {

def init(): Unit = {
println("call init()...")
}

def main(args: Array[String]): Unit = {
lazy val property = init() //用Lazy修饰
println("after init()")
println(property)
}

}
/*
执行结果
after init()
call init()...
()
*/