仅供参考,这是我在 StackOverflow 上的第一个问题,而且我对 Kotlin 还很陌生。
在开发一个完全使用 Kotlin(版本 1.1.3-2)的项目时,我在以下代码中看到一条警告(以及给好奇的小伙子们的评论):
// Code below is to handle presses of Volume up or Volume down.
// Without this, after pressing volume buttons, the navigation bar will
// show up and won't hide
val decorView = window.decorView
decorView
.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
decorView.systemUiVisibility = flags
}
}
该警告是为了可见性和 View.SYSTEM_UI_FLAG_FULLSCREEN === 0,它说不推荐使用 Int 和 Int 类型参数的恒等式.
我应该如何更改代码以及为什么它首先被弃用(为了知识起见)?
您可以使用以下方法更改代码结构平等 https://kotlinlang.org/docs/reference/equality.html#structural-equality如下所示:
// use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
decorView.systemUiVisibility = flags
}
为什么不建议使用参照平等 https://kotlinlang.org/docs/reference/equality.html#referential-equality?你可以看到我的回答here https://stackoverflow.com/questions/45139381/kotlin-boxed-int-are-not-the-same/45144516#45144516.
另一方面,当您使用参照/同一性平等 https://kotlinlang.org/docs/reference/equality.html#referential-equality也许会回来false
, 例如:
val ranged = arrayListOf(127, 127)
println(ranged[0] === ranged[1]) // true
println(ranged[0] == ranged[1]) // true
val exclusive = arrayListOf(128, 128)
// v--- print `false` here
println(exclusive[0] === exclusive[1]) // false
println(exclusive[0] == exclusive[1]) // true
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)