在该部分:Properties and Fields对kotlin的引用,写了下面的例子:
var allByDefault: Int? // error: explicit initializer required, default getter and setter implied
但是,我测试了代码,编译运行没有错误。 这是我的代码”
fun main(args:Array<String>){
var allByDefault:Int?
}
那么文档为什么会这样写:
error: explicit initializer required, default getter and setter implied
我已经在谷歌上搜索了帮助,但没有找到任何可以帮助我的结果。
@toniedzwiedz 的回答解决了这个问题。我的错。我弄错了属性和变量。
最佳答案
fun main(args:Array<String>){
var allByDefault:Int?
}
您在这里拥有的是 main
方法本地的 var
,而不是属性。
class MyClass {
//this is a property of MyClass that requires some means of initialization
var allByDefault: Int? // Error: Property must be initialized or be abstract
fun foo() {
var local: Int? // this is a local variable defined in the scope of foo, which is fine
// ...
}
}
关于kotlin - `var allByDefault: Int ?`会导致错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47959802/