本文介绍了在Kotlin中,get()的作用是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的新手,想知道 get()= login_email.txt.toString()做什么?

Im new to Kotlin and wonder what does the get() = login_email.txt.toString() do?

它是否设置了 email 字符串?

推荐答案

get() set(value)后,表示自定义getter的声明和/或二传手.这是使用默认值的基本示例:

get() and set(value) after a field means the declaration of a custom getter and/or setter. Here's a basic example using default values:

class Demo{
    var something: String
        get() = field
        set(value) {
            field = value;
        }
    constructor(something: String){
        this.something = something;
    }
}

但这两个是多余的.除非您要对其进行自定义操作,否则您实际上并不需要它们.它们是为vars自动添加的,尽管仅适用于 val 的getter(因为它们不能更改,因此没有setter).

These two are, however, redundant. You don't actually need them unless you're doing something custom with it. They're automatically added for vars, though that only applies to getters for vals (because they can't be changed, they don't have setters).

您要询问的那一行是一个自定义的吸气剂.

The line you were asking about is a custom getter.

get() // declares a custom getter
    = // if you don't know how this works, see my explanation below
    login_email.text.toString() // you should be familiar with this part already; gets the string value of the field

如果您不熟悉语法,则相当于没有 = 的等效项:

If you're not familiar with the syntax, this is the equivalent without =:

get(){
    return login_email.text.toString()
}

如果您有一个退货,则可以将方括号和return关键字替换为 = .如果可以帮助您记住,只需记住使用 = (正文+ return 关键字)的替代方法

if you have a single return, you can replace the brackets and return keyword with =. If it helps you remember, just remember the alternative to using = (a body + the return keyword)

TL; DR::它声明了一个自定义设置器,该设置器返回TextView/EditText的值(不确定是哪个,您没有在问题中包括该值)

TL;DR: it declares a custom setter that returns the value of a TextView/EditText (not sure which it is, you didn't include that in the question)

在您的情况下,您使用的是自定义getter或setter来处理属性数据.字段本身实际上不包含任何数据,但是您具有用于其他对象的吸气剂.

In your case, you're using a custom getter or setter to handle property data. The fields themselves don't actually contain any data, but you have getters for a different object.

以这个为例:

class Demo(private val someObject: MyCustomObjectWithSomeData){
    val text: String
        get() = someObject.text
    ... same for other stuff. Could also have setters, if the properties are mutable
}

这里的对象是私有的,但是对于这个问题它可以是公共的或内部的.

Here the object is private, but it could be public or internal for that matter.

Kotlin支持许多自定义getter.对于实例,您可以声明一个字段以显示私有变量的特定字段.例如,您有电子邮件.它不需要是变量,因为您有一个自定义的getter,并且该字段未初始化.如果将 var email 更改为 val ,则可以将其设置为非空:

Kotlin supports quite a lot with custom getters. For an instance, you can declare a field to display specific fields of a private variable. For an instance, in your case, you have the email. It doesn't need to be a variable, since you have a custom getter, and the field isn't initialized. If you change var email to a val, you can make it non-null:

val email: String
    get() = login_email.text.toString()

这也有助于实现空安全性.

That also helps with null-safety.

对于 error 字段,它稍微复杂一些.它不能是 val ,因为您声明了一个自定义的setter,但是如果添加了getter,则可以将其设置为非null:

And for the error field, it's slightly more complicated. It can't be a val because you declare a custom setter, but if you add a getter, you can make it non-null:

var error: String
    get() = login_error.text.toString()
    set(value){
        login_error.text = value;
    }

这篇关于在Kotlin中,get()的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:53