[英]Java Generic: How to extend a generic class of another generic class
First, I have a simple template, nothing fancy: 首先,我有一个简单的模板,没什么花哨的:
public abstract class ListOfK<K> {
private List<K> insides = new ArrayList<>();
}
Next, I'm creating a service interface using the template 接下来,我将使用模板创建服务接口
public abstract interface SomeService<K extends ListOfK<K>> {
int calculateSomething (K input);
int calculateAnother (ListOfK<K> list);
}
So far so good with the abstraction. 到目前为止,抽象效果很好。
Now, let's get to the implementation 现在,让我们开始实施
public class ListOfString extends ListOfK<String> {
}
and implementation of SomeService: 和实现SomeService:
public class SomeServiceImpl extends SomeService<String> {
@Override
public int calculateSomething(String input) {
return 0; // TODO impl
}
@Override
public int calculateAnother(ListOfK listOfK) {
return 0; // TODO impl
}
}
Somehow, when SomeServiceImpl
extends SomeService<String>
, it marks Type parameter java.lang.String is not within its bound; should extend ListOfK<String>
不知何故,当SomeServiceImpl
扩展SomeService<String>
,它表示Type parameter java.lang.String is not within its bound; should extend ListOfK<String>
Type parameter java.lang.String is not within its bound; should extend ListOfK<String>
What should I input as implementation of SomeService
so it doesn't give error? 我应该输入什么作为SomeService
实现,以便它不会出错? Or do I make mistake with SomeService? 还是使用SomeService出错? I just want a class whom input is a another class using Generic. 我只想要一个使用通用的输入是另一个类的类。
Thanks in advance. 提前致谢。
The key insight here is that K
in the context of the ListOfK
class is not the same type as K
in the context of the SomeService
class. 这里的关键结论是K
在上下文ListOfK
类是不一样的类型K
在上下文SomeService
类。 In fact, in your definition of SomeService
, you're saying that K
needs to itself be a ListOfK
, which I suspect is your problem. 实际上,在您对SomeService
的定义中,您说的是K
本身必须是ListOfK
,我怀疑这是您的问题。
calculateSomething
need to take (1) a single value, or (2) a ListOfK
of values? calculateSomething
是否需要(1)一个值,或(2)一个ListOfK
值? calculateAnother
need to take (1) a ListOfK
of values, or (2) a ListOfK
of ListOfK
values? 是否calculateAnother
需要采取(1)一个ListOfK
值,或(2)一个ListOfK
的ListOfK
值? I think you want option 1, but you've actually chosen option 2. 我认为您需要选项1,但实际上已经选择了选项2。
To fix this, don't constrain K
in SomeService
: 要解决此问题,请不要在SomeService
限制K
:
public interface SomeService<K> {
int calculateSomething(K input);
int calculateAnother(ListOfK<K> list);
}
The issue lies in the way you are defining the SomeService
interface. 问题在于您定义SomeService
接口的方式。 Your code of <K extends ListOfK<K>>
says that "we need a class K
which extends ListOfK
, which uses K
as its type parameter." 您的<K extends ListOfK<K>>
说:“我们需要一个扩展ListOfK
的类K
, ListOfK
使用K
作为其类型参数。” So that class would have to look something like: 因此该类必须看起来像:
public class Test extends ListOfK<Test> {
Which doesn't make any sense. 这没有任何意义。 It shouldn't only use itself inside of the list. 它不仅应该在列表中使用自己。 If you change the interface to something like: 如果您将界面更改为以下内容:
public abstract interface SomeService<K extends ListOfK<?>> {
It should fix your issue. 它应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.