简体   繁体   English

通用接口实现不起作用

[英]Generic interface implementation not working

My interface is: 我的界面是:

public interface SalesForceRecordService<T extends SalesForceRecord> {

    T update(T record) throws IOException;
}

My implimentation class is: 我的实现类是:

public class SalesForceRecordServiceImpl<T extends SalesforceRecordImpl> implements SalesforceRecordService {

  @Override
    public T update(T record) throws IOException {
        return null;
    }

}

@Override gives error, method does not override method from its superclass. @Override给出错误,方法不会从其超类覆盖方法。

I can't able to understand what is the problem. 我无法理解问题所在。

You have to pass a type to the super class because it expects one. 您必须将类型传递给超类,因为它需要一个。 If you want your SalesForceRecordServiceImpl to also be generic like you posted, then pass the T to SalesForceRecordService . 如果您希望SalesForceRecordServiceImpl也像您发布的一样通用,则将T传递给SalesForceRecordService

Try this: 尝试这个:

public class SalesForceRecordServiceImpl<T extends SalesForceRecordImpl>
        implements SalesForceRecordService<T> {

    @Override
    public T update(T record) throws IOException {
        return null;
    }
}

Also judging by its name SalesForceRecordImpl looks to me like a concrete implementation, so maybe you just want this: 同样以其名称判断, SalesForceRecordImpl在我看来像是一个具体的实现,所以也许您只想要这样:

public class SalesForceRecordServiceImpl
        implements SalesForceRecordService<SalesForceRecordImpl> {

    @Override
    public SalesForceRecordImpl update(SalesForceRecordImpl record)
            throws IOException {

        return null;
    }
}

Firstly on the implementing class, you should also pass the type template into the interface description. 首先,在实现类上,还应该将类型模板传递到接口描述中。 Otherwise the compiler has no way of checking the T supplied on SalesForceRecordServiceImpl is compatible with that of SalesforceRecordService 否则,编译器无法检查SalesForceRecordServiceImpl上提供的T是否与SalesforceRecordService兼容。

public class SalesForceRecordServiceImpl<T extends SalesforceRecordImpl> 
  implements SalesforceRecordService<T> {
   ...

(I'm assuming SalesforceRecordImpl is a subtype of SalesforceRecord) (我假设SalesforceRecordImpl是SalesforceRecord的子类型)

Secondly, forcing method to implement an interface using @Overrides annotation only available from java 6 onwards, so if you're on java 5 you'll get a compiler error 其次,强制方法使用@Overrides注释实现接口,该方法仅从Java 6起可用,因此,如果您使用的是Java 5,则会出现编译器错误

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

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