为变量分配下划线.下划线在做什么? [英] Assign a underscore to a variable. What is the underscore doing?
本文介绍了为变量分配下划线.下划线在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近我遇到了这样的代码:
Recently I've run into code like this:
var myVariable: variableKind = _
这似乎是一种将 null
分配给 myVariable
的方法.
It seems to be a way to assign null
to myVariable
.
谁能解释一下这种情况下 _
背后的原理?将 _
和 null
分配给变量有什么区别?
Can anyone explain the rationale behind _
in this case?
What are the differences between assigning _
and null
to a variable?
推荐答案
它用它的默认值初始化变量 - 这个值取决于类型.对于数字类型,这是 zero
,false
是布尔值,()
是 Unit
和 null 扩展 <代码>AnyRef.
It initialises the variable with it's default value - this value depends on the type. For numeric types, this is zero
, false
for booleans, ()
for Unit
and null for types extending AnyRef
.
这篇关于为变量分配下划线.下划线在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文