简体   繁体   English

“……的返回类型不是被重写成员的返回类型的子类型”吗?

[英]“Return type of … is not a subtype of the return type of the overridden member”?

I am trying to subclass a function ActionableNotification.java which reads similar to this: 我试图将一个类似于以下内容的函数ActionableNotification.java子类化:

package com.venmo.notifications.notifications;

import android.content.ContextWrapper;
import com.squareup.picasso.Target;

public abstract class ActionableNotification extends ContextWrapper implements Target {

    @NonNull
    protected abstract String getTrackingNotificationCategory();
    // ...
}

The inheriting class reads similar to this 继承类的内容与此类似

class DeclineInsufficientFundsBalanceOnlyCardNotification(context : Context, intent : Intent) : ActionableNotification(context, intent) {

    override fun getTrackingNotificationCategory() = ""
    // ...
 }

However, when I try to compile I get this error: 但是,当我尝试编译时,出现此错误:

/Users/kupeek/dev/venmo-android/p2p-app/src/main/java/com/venmo/notifications/notifications/DeclineInsufficientFundsBalanceOnlyCardNotification.kt: (21, 53): Return type of 'getTrackingNotificationCategory' is not a subtype of the return type of the overridden member '@NonNull protected/ protected and package / abstract fun getTrackingNotificationCategory(): String defined in com.venmo.notifications.notifications.ActionableNotification' /Users/kupeek/dev/venmo-android/p2p-app/src/main/java/com/venmo/notifications/notifications/DeclineInsufficientFundsBalanceOnlyCardNotification.kt:(21,53):返回类型“ getTrackingNotificationCategory”不是其子类型重写成员的返回类型'@NonNull protected / protected和package / abstract fun getTrackingNotificationCategory():com.venmo.notifications.notifications.ActionableNotification中定义的字符串

I don't understand this error message, because the return type of the Kotlin inheriting class is a string, as is the base class? 我不明白此错误消息,因为Kotlin继承类的返回类型是字符串,基类也是如此?

Maybe annotating the abstract method with the JetBrains annotation @NotNull will be better. 也许用JetBrains注释@NotNull注释抽象方法会更好。 I found it taking a look a this . 我发现它采取了看起来这个 Hope this may help you. 希望这对您有帮助。

尽管我不完全确定为什么这样做,但是当我为覆盖函数指定String类型而不是依靠类型推断时,错误消失了:

override fun getTrackingNotificationCategory(): String = ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM