Java泛型类型在方法签名中不匹配 [英] Java generics type mismatch in method signature
问题描述
我有一个 Executor
类,它调用接口 IService< T>
的实例,其中 KeyList< T>
参数。
class Executor {
KeyList<?> _keys;
IService<?> _服务;
public Executor(IService??> service,KeyList<?> key){
_service = service;
_keys = keys;
}
public void execute(){
_service.invoke(_keys);
interface IService< T> {
public void invoke(KeyList< T> keys);
}
class KeyList< T> {
列表< T> _list;
}
我使用<?>
对于Executor的成员,因为它不关心IService和KeyList是如何被参数化的,但是下面会产生一个编译错误,说明这些参数不适用:
public void execute(){
_service.invoke(_keys); //错误调用
}
我猜这是抱怨,因为 KeyList <?>
不等于 KeyList
,但 <?>< code>与
相同<?扩展对象>
,所以我有点困惑。有没有更好的选择?
A 通配符(?
)代表某种特定的未知类型。但是你在这里处理两个单独的通配符 - 它们可能不一样。改为使用以下内容:
class Executor< T> {
KeyList< T> _keys;
IService< T> _服务;
public Executor(IService< T> service,KeyList< T> key){
_service = service;
_keys = keys;
}
public void execute(){
_service.invoke(_keys);
$ p $这个声明了一个类型参数对于 Executor
类来说,T
,然后用它作为 _keys
的类型参数, _service
,确保它们兼容。
如果您不能参数化 Executor
,尝试使用参数化的辅助类:
class Executor {
private静态最终类ServiceAndKeys< T> {
private final KeyList< T>键;
private final IService< T>服务;
ServiceAndKeys(IService< T> service,KeyList< T> keys){
this.service = service;
this.keys = keys;
}
void execute(){
service.invoke(keys);
}
}
private final ServiceAndKeys<?> serviceAndKeys;
public< T>执行者(IService T服务,KeyList T密钥){
serviceAndKeys = new ServiceAndKeys< T>(service,keys);
}
public void execute(){
serviceAndKeys.execute();
}
}
I have an Executor
class that invokes instances of interface IService<T>
with KeyList<T>
argument.
class Executor{
KeyList<?> _keys;
IService<?> _service;
public Executor(IService<?> service, KeyList<?> keys){
_service = service;
_keys = keys;
}
public void execute(){
_service.invoke(_keys);
}
}
interface IService<T>{
public void invoke( KeyList<T> keys);
}
class KeyList<T> {
List<T> _list;
}
I used <?>
for the Executor's members since it does not care how IService and KeyList are parameterized, but the following raises a compilation error saying the arguments are not applicable:
public void execute(){
_service.invoke(_keys); //error on invoke
}
I'm guessing it's complaining becuase KeyList<?>
is not equal to KeyList<T>
, but <?>
is the same as <? extends Object>
, so I'm a bit confused. Is there a better alternative?
解决方案 A wildcard (?
) represents some specific unknown type. But you're dealing with two separate wildcards here - they may not be the same. Use the following instead:
class Executor<T> {
KeyList<T> _keys;
IService<T> _service;
public Executor(IService<T> service, KeyList<T> keys){
_service = service;
_keys = keys;
}
public void execute(){
_service.invoke(_keys);
}
}
This declares a type parameter T
for the class Executor
, and then uses it as a type argument for _keys
and _service
, ensuring they're compatible.
If you can't parameterize Executor
, try using a parameterized helper class:
class Executor {
private static final class ServiceAndKeys<T> {
private final KeyList<T> keys;
private final IService<T> service;
ServiceAndKeys(IService<T> service, KeyList<T> keys) {
this.service = service;
this.keys = keys;
}
void execute() {
service.invoke(keys);
}
}
private final ServiceAndKeys<?> serviceAndKeys;
public <T> Executor(IService<T> service, KeyList<T> keys){
serviceAndKeys = new ServiceAndKeys<T>(service, keys);
}
public void execute() {
serviceAndKeys.execute();
}
}
这篇关于Java泛型类型在方法签名中不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!