解释这个Kotlin函数结构 [英] Explain this Kotlin function structure

查看:139
本文介绍了解释这个Kotlin函数结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个Kotlin函数。我知道我们有一个名为 mPasswordView !!。setOnEditorActionListener 的函数,它需要参数 TextView.OnEditorActionListener ,但那是什么之后呢?我们在参数里面有大括号?

I am working with this Kotlin function. I know that we have a function called mPasswordView!!.setOnEditorActionListener, that take parameter TextView.OnEditorActionListener, but what's that after it? we have curly brackets inside parameter?

mPasswordView!!.setOnEditorActionListener(TextView.OnEditorActionListener { textView, id, keyEvent ->
    if (id == R.id.login || id == EditorInfo.IME_NULL) {
        attemptLogin()
        return@OnEditorActionListener true
    }
    false
})


推荐答案

示例中使用的功能是 SAM构造函数 setOnEditorActionListener 侦听器将 OnEditorActionListener 作为其参数。此接口只有一个必须实现的方法,这使得它成为单一抽象方法(SAM)接口。

The feature used in your example is a SAM constructor. The setOnEditorActionListener listener takes an OnEditorActionListener as its parameter. This interface only has a single method that you have to implement, which makes it a Single Abstract Method (SAM) interface.

在Java中使用此方法的完整语法be:

The full syntax for using this method in Java would be:

mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        attemptLogin();
        return true;
    }
});

与Kotlin的一对一转换会给您:

The one-to-one conversion to Kotlin would give you:

mPasswordView.setOnEditorActionListener(object: TextView.OnEditorActionListener{
    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        attemptLogin()
        return true
    }
})


$ b $但是,Kotlin允许您使用以更简洁的语法将SAM接口作为其参数的方法,而不是传递lambda。这被称为SAM转换:

Kotlin, however, allows you to use methods that take SAM interfaces as their parameter with a more concise syntax, by passing in a lambda instead. This is called a SAM conversion:

mPasswordView.setOnEditorActionListener { v, actionId, event ->
    attemptLogin()
    true
}

SAM转换自动确定这个lambda对应于哪个接口,但是你可以通过使用称为SAM构造函数的东西显式地指定它,这就是你的示例代码中的内容。 SAM构造函数返回一个实现给定接口的对象,并将传递给它的lambda传递给它的单个方法的实现。

SAM conversions automatically determine which interface this lambda corresponds to, but you can specify it explicitly by using something called a SAM constructor, this is what's in your sample code. A SAM constructor returns an object that implements the given interface, and makes the lambda you've passed to it its single method's implementation.

mPasswordView.setOnEditorActionListener( TextView.OnEditorActionListener { v, actionId, event ->
    attemptLogin()
    true
})

在这种情况下,这是多余的,因为只有一个方法叫做 setOnEditorActionListener 。但是,如果有多个具有相同名称的方法,它们使用不同的接口作为参数,则可以使用SAM构造函数来指定要调用的方法的哪个重载。

This is redundant in this specific situation, because there is only one method called setOnEditorActionListener. But if there were multiple methods with this same name, that took different interfaces as parameters, you could use the SAM constructor to specify which overload of the method you want to call.

有关SAM转换的官方文档

这篇关于解释这个Kotlin函数结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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