Kotlin的支持领域有什么用? [英] What's Kotlin Backing Field For?

查看:106
本文介绍了Kotlin的支持领域有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Java开发人员,后备字段的概念对我来说有点陌生.鉴于:

As a Java developer, the concept of a backing field is a bit foreign to me. Given:

   class Sample {
        var counter = 0 // the initializer value is written directly to the backing field
        set(value) {
            if (value >= 0) field = value
        }
    }

此支持领域有什么好处? Kotlin文档说: Kotlin中的类不能具有字段.但是,有时在使用自定义访问器时必须有一个后备字段.为什么?在setter中使用属性名称本身有什么区别,例如.

What's this backing field good for? Kotlin docs said: Classes in Kotlin cannot have fields. However, sometimes it is necessary to have a backing field when using custom accessors. Why? Whats the difference with using the properties name itself inside the setter, eg.

    class Sample {        
        var counter = 0
        set(value) {
            if (value >= 0) this.counter = value // or just counter = value?
        }
    }

推荐答案

因为,如果您没有field关键字,则将无法在get()中实际设置/获取值,或者set(value).它使您可以访问自定义访问器中的后备字段.

Because, say if you don't have field keyword, you won't be able to actually set/get the value in the get() or set(value). It enables you to access the backing field in the custom accessors.

这是您的示例的等效Java代码:

This is the equivalent Java code of your sample:

class Sample {
    private int counter = 0;
    public void setCounter(int value) {
        if (value >= 0) setCounter(value);
    }
    public int getCounter() {
        return counter;
    }
}

显然这不好,因为setter只是对自己的无限次递归,永远不会改变任何东西.请记住,在kotlin中,每当您编写foo.bar = value时,它将被转换为setter调用,而不是PUTFIELD.

Apparently this is not good, as the setter is just an infinte recursion into itself, never changing anything. Remember in kotlin whenever you write foo.bar = value it will be translated into a setter call instead of a PUTFIELD.

Java具有 fields ,而Kotlin具有 properties ,这是一个比字段更高层次的概念.

Java has fields while Kotlin has properties, which is a rather higher level concept than fields.

有两种类型的属性:一种具有后备字段,一种不具有后备字段.

There are two types of properties: one with a backing field, one without.

具有后备字段的属性将以字段的形式存储值.该字段使在内存中存储值成为可能.此类属性的一个示例是Pairfirstsecond属性.该属性将更改Pair在内存中的表示形式.

A property with a backing field will store the value in the form of a field. That field makes storing value in memory possible. An example of such property is the first and second properties of Pair. That property will change the in-memory representation of Pair.

没有后备字段的属性将不得不以其他方式存储其值,而不是直接将其存储在内存中.必须从其他属性或对象本身计算得出.这种属性的一个示例是Listindices扩展属性,该属性没有字段支持,而是基于size属性的计算结果.因此,它不会更改List的内存表示形式(由于Java是静态类型,所以它根本无法执行).

A property without a backing field will have to store their value in other ways than directly storing it in memory. It must be computed from other properties, or, the object itself. An example of such property is the indices extension property of List, which is not backed by a field, but a computed result based on size property. So it won't change the in-memory representation of List (which it can't do at all because Java is statically typed).

这篇关于Kotlin的支持领域有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆