Property must be initialized or be abstract

前端 未结 2 378
情话喂你
情话喂你 2021-02-01 11:17

It must be stupid question, but I\'m really new to Kotlin and I didn\'t find any solution.

How to declare class field? Like we can have it in java:

prote         


        
2条回答
  •  名媛妹妹
    2021-02-01 12:10

    If you'd like to initialize a property outside the constructor, then late-initialized properties is what you may be looking for. Declare the property with the lateinit modifier, which will allow to skip the otherwise required initializer and will make the property access fail with exception until some meaningful value is assigned to it:

    protected lateinit var sharedPreferences: SharedPreferences
    

提交回复
热议问题