gpt4 book ai didi

android - 修饰符 'const' 不适用于 'local variable' ,为什么?

转载 作者:行者123 更新时间:2023-12-04 23:59:32 24 4
gpt4 key购买 nike

当我尝试像这样放置 const val 时:

const val DRAWABLE_RIGHT : Int = 2

在一个方法中,我得到了错误:

Modifier 'const' is not applicable to 'local variable'

当我将这个 const val 移动到类的顶部时,如下所示:

class MyClass @JvmOverloads constructor(context: Context) : ConstraintLayout(context) {
const val DRAWABLE_RIGHT : Int = 2
...
}

编译器说它仍然不正确:

Const 'val' are only allowed on top level or in objects

顶级是什么意思?

最佳答案

顶级意味着您应该将变量放在类之外。

也可以将它放在伴随对象中。

关于android - 修饰符 'const' 不适用于 'local variable' ,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65612741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com