8

我对这个 Kotlin 错误感到困惑,该错误与为从 maven 包导入的抽象类提供实现相关联。

我有一个用 Kotlin 编写的 maven 库,它公开了一个名为 APIGatewayRequestHandler 的抽象类。在我导入库的应用程序中,我提供了抽象类的实现:

class GetWelcomeMessageHandler : APIGatewayRequestHandler<WelcomeMessage>()
    fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): WelcomeMessage {
        return WelcomeMessage()
    }
}

库中反编译的抽象类如下所示:

public abstract class APIGatewayRequestHandler<T> public constructor() : com.amazonaws.services.lambda.runtime.RequestHandler<com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, T> {
    public abstract fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): T

    public open fun handleRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent?, context: com.amazonaws.services.lambda.runtime.Context?): T {
        /* compiled code */
    }
}

我收到以下错误:

Class 'GetWelcomeMessageHandler' is not abstract and does not implement abstract base class member
public abstract fun handleAPIGatewayRequest(input: APIGatewayProxyRequestEvent, context: Context?): WelcomeMessage
4

1 回答 1

10

我认为您只是缺少一些override关键字。也就是说,您的抽象类应该在handleRequest方法上有它:

public abstract class APIGatewayRequestHandler<T> public constructor() : com.amazonaws.services.lambda.runtime.RequestHandler<com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, T> {
    public abstract fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): T

    public override fun handleRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent?, context: com.amazonaws.services.lambda.runtime.Context?): T {
        /* compiled code */
    }
}

然后你GetWelcomeMessageHandler应该在它的handleAPIGatewayRequest方法上有它:

class GetWelcomeMessageHandler : APIGatewayRequestHandler<WelcomeMessage>() { // <-- This curly brace was also missing
    override fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): WelcomeMessage {
        return WelcomeMessage()
    }
}
于 2018-06-06T05:02:48.427 回答