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

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

问题描述

我有以下界面:

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> {
}

然后对于您的具体类:

public class SpecificEntry extends AbstractEntry<SpecificEntryBean> {
}

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

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