在kotlin引用的属性和字段中,编写了以下示例:
var allByDefault:Int?//错误:需要显式初始化程序,隐含默认的getter和setter
但是,我测试代码,编译和运行没有错误.这是我的代码"
fun main(args:Array<String>){
var allByDefault:Int?
}
Run Code Online (Sandbox Code Playgroud)
那么为什么文档写道:
错误:需要显式初始化程序,隐含默认的getter和setter
我搜索谷歌寻求帮助,但没有找到任何可以帮助我的结果.
@toniedzwiedz的回答解决了这个问题.我的错.我误认为属性和变量.
fun main(args:Array<String>){
var allByDefault:Int?
}
Run Code Online (Sandbox Code Playgroud)
你在这里拥有的是方法的var本地main,而不是属性.
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
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |