简体   繁体   English

在Java中继承泛型接口

[英]Inheriting Generics Interface in Java

I have interface with generics: 我有泛型的接口:

public interface Cache<CachePrimaryKey extends Object, RowModel extends CacheRowModel>

I need to create child interface that extends Cache but adds new methods while keeping generics . 我需要创建扩展Cache的子接口,但在保持泛型的同时添加新方法。 What would be the correct syntax to retain generics part? 保留泛型部分的正确语法是什么?

I have implementation like. 我有类似的实现。

public class InfoCache implements Cache<Long, DataRowModel> 

I tried 我试过了

public interface EnrichedCache<CachePrimaryKey extends Object, 
       RowModel extends CacheRowModel> extends  Cache

but when I change interface for InfoCache-class from Cache to EnrichedCache, I am getting "not overriding methods from its superclass". 但是当我将InfoCache-class的接口从Cache更改为EnrichedCache时,我得到的是“没有覆盖其超类的方法”。 If I use "implement methods" in IntelliJ, instead of methods for Long+DataRowModel it offers methods for Object and CacheRowModel (which is not what I want). 如果我在IntelliJ中使用“实现方法”,而不是Long + DataRowModel的方法,它提供了Object和CacheRowModel的方法(这不是我想要的)。

What would be the correct way to inherit interface and retain all generics? 继承接口和保留所有泛型的正确方法是什么?

First things first, we use capital letters to represent generics, this is to avoid the following nonsense: 首先,我们使用大写字母来表示泛型,这是为了避免以下废话:

public interface Cache<String> {
    String getKey();
}

What do you thing the above interface does? 觉得上面的interface有什么作用?


So, rewriting your interface as: 因此,将您的interface重写为:

public interface Cache<K, R extends CacheRowModel>

note that extends Object is a nonsense, I have removed it 请注意, extends Object是一个废话,我删除了它

Then to create a generic interface that extends Cache and keeps the same generics we would do: 然后创建一个extends Cache的通用interface ,并保留相同的泛型:

public interface EnrichedCache<K, R extends CacheRowModel> extends Cache<K,R>

ie we have a generic interface that has parameters K (can be anything) and R (must be a subclass of CacheRowModel ) that extends Cache with the same parameters. 即我们有一个通用interface ,它具有参数K (可以是任何东西)和R (必须是CacheRowModel的子类),它使用相同的参数extends Cache


You can tighten the generics you pass on too: 您也可以收紧传递的泛型:

public interface NumericCache<K extends Number, R extends CacheRowModel> extends Cache<K,R>

Here we require the key for NumericCache to extends Number . 这里我们需要NumericCache的密钥来extends Number

You can also set one generic parameter to a concrete type and pass on the second: 您还可以将一个通用参数设置为具体类型并传递第二个参数:

public interface StringCache<R extends CacheRowModel> extends Cache<String, R>

Here we explicitly set the key of the inherited cache to String but allow the user to specify CacheRowModel to use. 这里我们将继承缓存的键显式设置为String但允许用户指定要使用的CacheRowModel

Your second interface extend the raw Cache. 您的第二个接口扩展了原始Cache。

Try this code: 试试这段代码:
public interface EnrichedCache<CachePrimaryKey extends Object, RowModel extends CacheRowModel> extends Cache<CachePrimaryKey, RowModel>

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

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