泛型:从实现接口的抽象类继承 [英] Generics: Inheriting from an abstract class that implements an interface

查看:825
本文介绍了泛型:从实现接口的抽象类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下界面:

public interface SingleRecordInterface<T> {
    public void insert(T object);
}

我在下面有一个抽象类(没有提到方法插入):

I have the abstract class below (that does not mention the method insert):

public abstract class AbstractEntry implements SingleRecordInterface<AbstractEntryBean> {
}

我有具体的课程:

public class SpecificEntry extends AbstractEntry {
    public void insert(SpecificEntryBean entry) {
        // stuff
    }
}

最后,SpecificEntryBean定义为:

Finally, SpecificEntryBean is defined as:

public class SpecificEntryBean extends AbstractEntryBean {
}

我遇到以下错误:

SpecificEntry类型必须实现继承的抽象方法SingleRecordInterface.insert(AbstractEntryBean)

The type SpecificEntry must implement the inherited abstract method SingleRecordInterface.insert(AbstractEntryBean)

鉴于SpecificEntryBean扩展了AbstractEntryBean,所以我不明白此错误的原因.如何解决此错误?

I don't understand the reason for this error, given that SpecificEntryBean extends AbstractEntryBean. How do I fix this error?

推荐答案

您还需要使抽象类通用:

You need to make your abstract class generic as well:

public abstract class AbstractEntry<T extends AbstractEntryBean> implements SingleRecordInterface<T> {
}

然后针对您的具体课程:

Then for your concrete class:

public class SpecificEntry extends AbstractEntry<SpecificEntryBean> {
}

这篇关于泛型:从实现接口的抽象类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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