无法理解如何在Kotlin中使用SAM接口 [英] Can't understand how to use SAM interface in Kotlin

查看:105
本文介绍了无法理解如何在Kotlin中使用SAM接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么以下代码有效: 山姆界面:

I can't understand why the following code works: Sam Interface:

    fun interface WebResponseHandler
{
    fun onWebResponseFinished(jsonObject:JSONObject)
}

和Implemnetaiton看起来像:

and Implemnetaiton looks like:

  val onWebResponseHandler: VolleyHandler.WebResponseHandler = VolleyHandler.WebResponseHandler()
{
    fun onWebResponseFinished(jsonObject: JSONObject) {
        Log.v("da", "dsa")
    }

    fun foo() {
        it.getString("name")
    }
}

foo 函数如何知道什么是?毕竟, foo 函数不是 VolleyHandler.WebResponseHandler 接口的一部分,并且仅在将接口作为一种方法使用时起作用. em> Foo 被调用而没有任何参数,那么为什么 it 的含义在那里?

How does foo function knows what is it? After all, foo function isn't part of the VolleyHandler.WebResponseHandler interface, and it works only when interface as only one method.Function Foo is being called without any arguments, so why is the meaning of it there?

此外,如果我在尝试实现接口时从接口声明中省略关键字 fun ,则会发生另一个问题:

In addition another problem occurs if I'm omitting the keyword fun from the interface declaration, when i'm trying to implement the interface:

val onWebResponseHandler: VolleyHandler.WebResponseHandler = VolleyHandler.WebResponseHandler()
{
    fun onWebResponseFinished(jsonObject: JSONObject) {
        Log.v("da", "dsa")
    }

    fun foo() {
        Log.v("something", "something")

    }
}

我收到一个编译错误提示

I'm getting a compilation error saying

接口WebResponseHandler没有构造函数

Interface WebResponseHandler does not have constructors

两种情况有什么不同?如果删除(),我会遇到很多不同的错误,例如:

what is the different between both situation? If i remove the () i'm getting a lot of different errors like :

函数声明必须具有名称

Function declaration must have a name

我希望有人能弄清楚这里出了什么问题

I hope someone can figure out what is wrong here

推荐答案

不是Kotlin一团糟,而是您使用了错误的语法.

It's not Kotlin that's a mess, it's that you're using the wrong syntax for what you want to do.

fun interface让您做的就是写

val onWebResponseHandler = VolleyHandler.WebResponseHandler {
    jsonObject: JSONObject -> Log.v("da", "dsa")
}

使用普通接口可以执行的操作是写

What you can do with a normal interface is write

val onWebResponseHandler = object : VolleyHandler.WebResponseHandler() {
    fun onWebResponseFinished(jsonObject: JSONObject) {
        Log.v("da", "dsa")
    }

    fun foo() {
        Log.v("something", "something")

    }
}

您的代码不执行任何操作,并且在编译时不会执行您所期望的操作.你写的等同于

Your code does none of those things, and when compiled, doesn't do anything like what you mean it to. What you wrote is equivalent to

val onWebResponseHandler = VolleyHandler.WebResponseHandler() { 
  unusedJsonObject: JSONObject ->
    fun aFunctionWhichIsDefinedAndNeverCalled(jsonObject: JSONObject) {
        Log.v("da", "dsa")
    }

    fun anotherFunctionWhichIsDefinedAndNeverCalled() {
        unusedJsonObject.getString("name")
    }
    // doesn't actually do anything at all when executed
    // just defines functions which aren't ever invoked
}

...之所以进行编译是因为,由于它是一个fun interface,因此您可以只是将一个lambda传递给构造函数,但实际上并没有做任何与您期望的类似的事情.

...which compiles because, since it's a fun interface, you can just pass a lambda to the constructor, but doesn't actually do anything resembling what you expect.

这篇关于无法理解如何在Kotlin中使用SAM接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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