如何在Kotlin中创建空的类对象?

huangapple go评论61阅读模式
英文:

How can I create null class object in Kotlin?

问题

在Java中,我们可以像这样声明一个空的类对象:

VideoRecord videoRecord;

之后,我们可以检查它是否为null:

if (videoRecord == null){
    videoRecord = new VideoRecord();
}

我对Kotlin还不熟悉,我想在Kotlin中实现相同的功能。我尝试了下面的代码:

lateinit var videoRecord1: VideoRecord

if (videoRecord1 == null){
    videoRecord1 = VideoRecord()
}

但是它给了我一个警告,说条件 videoRecord1 == null 总是为false。我该如何在Kotlin中实现相同的功能呢?

英文:

In java, we can declare a null class object like this

VideoRecord videoRecord;

After that, we can check if it is null or not

if (videoRecord == null){
    videoRecord = new VideoRecord();
}

I am new to Kotlin and I would like to do the same thing in Kotlin. I have tried this code below.

lateinit var videoRecord1:VideoRecord

if (videoRecord1 == null){
    videoRecord1 = VideoRecord()
}

But it gives me a warning like the condition videoRecord1 == null is always false. How can I make the same thing in Kotlin?

答案1

得分: 5

你可以像这样初始化它的变量

可空引用https://kotlinlang.org/docs/reference/null-safety.html
英文:
var videoRecord: VideoRecord? = null

you can initial its variable like this

nullable reference : https://kotlinlang.org/docs/reference/null-safety.html

答案2

得分: 2

根据您想要实现的目标,例如惰性初始化,这可能比 andika_kurniawan 给出的答案更适合您:

val videoRecord: VideoRecord by lazy { VideoRecord() }

这将在首次访问时惰性初始化 videoRecord

这种方式的优势在于,访问 videoRecord 时无需检查是否为 null,因为它不可能null。这显著简化了该变量的使用。此外,您可以将 videoRecord 标记为 val,这意味着它是 final,因此不能被覆写。

而 @andika_kurniawan 给出的示例:

var videoRecord: VideoRecord? = null

具有一些注意事项和缺点:

  1. 始终需要检查 videoRecord 是否为 null(这很繁琐),请参阅以下示例:

    if (videoRecord != null) {
        videoRecord.callAMethod()
    }
    

    以上示例将不起作用,因为 videoRecord 被定义为可为空,所以您需要使用 !! 运算符告诉 Kotlin 您确定该变量不为 null:

    if (videoRecord != null) {
        videoRecord!!.callAMethod()
    }
    

    您当然可以使用其他惯用法,例如空安全操作符 ?.

    videoRecord?.callAMethod()
    

    或者前面提到的 !! 运算符(在为 null 时引发异常),但这次不需要进行空检查:

    videoRecord!!.callAMethod()
    

    您还可以使用局部变量,这会稍微简化使用方法:

    val videoRecord = videoRecord
    if (videoRecord != null) {
        videoRecord.callAMethod()
    }
    
  2. 该变量不是 final,因此没有任何限制阻止您在代码中的某处执行以下操作:

    videoRecord = null
    
  3. 您必须在某个地方进行初始化,因此如果有多个方法访问 videoRecord,则首先必须在尚未初始化的情况下初始化它,引入不必要的冗余。

英文:

Depending on what you're trying to achieve, e.g. lazy initialization then this may be more suitable for you than the answer given by andika_kurniawan:

val videoRecord: VideoRecord by lazy { VideoRecord() } 

This will lazily initialize videoRecord the first time it is accessed.

The advantage of this way is that you don't have to check for null when accessing videoRecord, because it cannot be null. This simplifies the usage of that variable significantly. Additionally you can mark videoRecord as val, meaning it is final, so it cannot be overwritten.

The example shown by @andika_kurniawan:

var videoRecord: VideoRecord? = null

Has some caveats and disadvantages:

  1. You always have to check that videoRecord is not null (and it gets tedious), see this example:

    if (videoRecord != null) {
        videoRecord.callAMethod()
    }
    

    The above will not work, because videoRecord defined as nullable, so you need to use the !! operator to tell kotlin that you're sure that the variable is not null:

    if (videoRecord != null) {
        videoRecord!!.callAMethod()
    }
    

    You can of course use other idioms, like the null-safe operator ?.

    videoRecord?.callAMethod()
    

    Or the already mentioned !! operator (which throws an exception when it is null) but this time without a null check:

    videoRecord!!.callAMethod()
    

    You may also use local variables, which simplify the usage (a little bit):

    val videoRecord = videoRecord
    if (videoRecord != null) {
        videoRecord.callAMethod()
    }
    
  2. The variable is not final, so nothing stops you from doing this somewhere in your code:

    videoRecord = null
    
  3. You have to initialize it somewhere, so if you have multiple methods accessing videoRecord you first have to initialize it if it hasn't already been, introducing unnecessary redundancy.

答案3

得分: 1

在Kotlin中,变量的声明与Java不同,因为Kotlin是一种空安全语言。您必须将变量声明为可空。只有这样,它的值才能为null。

您可以按以下方式声明:

var videoRecord: VideoRecord? = null

要访问可空值,您必须在变量名称后面使用 !! 或 ?。

英文:

In kotlin, declaration of variable is different from java as kotlin is null safe language. You have to declare variable nullable. Only then its value can be null.

You can declare it as below

var videoRecord: VideoRecord? = null

To access nullable values you have to use !! or ? with variable names.

huangapple
  • 本文由 发表于 2020年10月13日 19:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64334025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定