类不是抽象的,也不实现抽象基类成员

时间:2018-06-06 04:54:55

标签: kotlin kotlin-interop

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

我有一个用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

1 个答案:

答案 0 :(得分:2)

我认为您错过了一些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()
    }
}