[英]Java generic abstract class return type mismatch
I have an abstract class in java: 我在Java中有一个抽象类:
private abstract class Importable<T> {
// ...
public abstract List<T> validateData(List<Path> paths);
public abstract void importData(List<T> data);
// ...
}
private static class MyImportable extends Importable<String>{
public List<String> validateData(List<Path> paths){
return // valid ArrayList<String>;
}
public void importData(List<String> data){
// do stuff with data
}
}
Now, if I do this: 现在,如果我这样做:
public Importable importable;
// ...
importable = new MyImportable(); // MyImportable extends Importable<String>
calling this works: 调用此工程:
importData(validateData(myPaths));
But I don't like the raw Importable
, so I added <?>
但是我不喜欢原始的
Importable
,所以我添加了<?>
public Importable<?> importable;
doing so throws error: 这样做会引发错误:
The method importData(List<capture#5-of ?>) in the type ImportController.Importable<capture#5-of ?> is not applicable for the arguments (List<capture#6-of ?>)
while executing importData(validateData(myPaths));
在执行
importData(validateData(myPaths));
and eclipse suggests me to cast to List<?>
, which did nothing to solve the problem 蚀建议我将其转换为
List<?>
,这并不能解决问题
Is there something I'm missing? 有什么我想念的吗?
EDIT: sorry if I wasn't clear, here is the actual usage 编辑:对不起,如果我不清楚,这是实际用法
I will have many class that extends Importable with different types: 我将有许多扩展了Importable的类,它们具有不同的类型:
Importable importable;
switch(condition){
case 1:
importable= // Importable<MyType1>
break;
case 2:
importable= // Importable<MyType2>
// ....
}
importData(validateData(myPaths));
EDIT2: EDIT2:
I intentionally separate importData
and validateData
because some other class might want to validateData
only 我故意将
importData
和validateData
分开,因为其他一些类可能只希望validateData
If matters: I'm using eclipse 4.3 w/ Java 8 on Windows 7 x64 如果重要:我正在Windows 7 x64上使用带有Java 8的Eclipse 4.3
Replace the generic argument ?
替换通用参数
?
with String
带
String
EDIT (taking the edited question into account): 编辑 (将已编辑的问题考虑在内):
I think that the following would be a better design: 我认为以下是更好的设计:
switch(condition){
case 1:
validateAndImport(someImportable, paths); //Importable<MyType1>
break;
case 2:
validateAndImport(someOtherImportable, paths); //Importable<MyType1>
}
... ...
<T> void validateAndImport(Importable<T> importable, List<Path> paths) {
importable.importData(importable.validateData(myPaths))
}
This will solve your problem 这将解决您的问题
public Importable<String> importable;
// ...
importable = new MyImportable(); // MyImportable extends Importable<String>
as you know MyImportable
extends Importable<String>
如您所知
MyImportable
extends Importable<String>
MyImportable
extends Importable<String>
--EDIT-- - 编辑 -
as per you updated question, please try this one 根据您更新的问题,请尝试此问题
abstract class Importable<T> {
public abstract List<T> validateData(List<Path> paths);
public abstract void importData(List<? extends Object> list);
}
and use 和使用
public Importable<? extends Object> importable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.