[英]Can not cast class to generics in java
Please help resolve an issue regarding generics. 请帮助解决有关泛型的问题。 I tried many ways but it's still not working.
我尝试了很多方法,但仍然无法正常工作。 Problem is:
问题是:
public static void main(String[] args) {
Utils.execute(new TestAction(), new TestCallBack());
}
Compiler show error: 编译器显示错误:
The method execute(Action<?>, CallBack<?,Action<?>>) in the type Utils is not applicable for the arguments (ImplementClass.TestAction, ImplementClass.TestCallBack)
My classes is: 我的课程是:
Action class: 动作类:
public abstract class Action<R> {
public R getResult() {
return null;
}
}
TestAction class is: TestAction类是:
class TestAction extends Action<String> {
@Override
public String getResult() {
return super.getResult();
}
}
Callback class is: 回调类为:
public interface CallBack<R, A extends Action<R>> {
public void onCall(A action);}
TestCallback class is: TestCallback类为:
class TestCallBack implements CallBack<String, TestAction> {
@Override
public void onCall(TestAction action) {
}
}
And Utils class is: 实用程序类是:
public class Utils {
public static void execute(Action<?> action, CallBack<?, Action<?>> callback) {
}
}
Thanks a lot. 非常感谢。
The second parameter of the execute
method is CallBack<?, Action<?>>
, and Action
there means the Action
class itself, subclass of it is not allowed. execute
方法的第二个参数是CallBack<?, Action<?>>
和Action
那里的Action
表示Action
类本身,而不是其子类。 What you need there is - ? extends Action<?>
您需要的是-
? extends Action<?>
? extends Action<?>
, which means either Action
or some subclass of it. ? extends Action<?>
,这意味着Action
或其子类。
Try changing the method signature - 尝试更改方法签名-
public static void execute(Action<?> action, CallBack<?, ? extends Action<?>> callback) {
Note: 注意:
Generics are not co-variant. 泛型不是协变的。 Take for example a method as follows -
例如,采取以下方法-
static void method(List<Object> l) {}
And an invocation as follows is not allowed - 并且不允许进行以下调用-
method(new ArrayList<String>());
You need to change two things, 您需要更改两件事,
TestCallBack should be like this - TestCallBack应该像这样-
public static class TestCallBack implements CallBack<String, Action<String>> {
@Override
public void onCall(Action<String> action) {
}
}
and, Utils should be like this - 而且, 实用程序应该是这样的-
public static class Utils {
// You need to ensure the same type, not just try and accept anything.
public static <T> void execute(Action<T> action, CallBack<?, Action<T>> callback) {
}
}
or using inner classes of a class called Question
- 或使用称为
Question
的类的内部类-
public abstract class Action<R> {
public R getResult() {
return null;
}
}
public class TestAction extends Action<String> {
@Override
public String getResult() {
return super.getResult();
}
}
public interface CallBack<R, A extends Action<R>> {
public void onCall(A action);
}
public class TestCallBack implements CallBack<String, TestAction> {
@Override
public void onCall(TestAction action) {
}
}
public class Utils {
public void execute(Action<?> action, CallBack<?, ? extends Action<?>> callback) {
}
}
public static void main(String[] args) {
Question question = new Question();
question.new Utils().execute(question.new TestAction(), question.new TestCallBack());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.