类型安全:从对象取消选中投射 [英] Type safety: Unchecked cast from Object
本文介绍了类型安全:从对象取消选中投射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将一个对象投射到我的Action类,但会产生一个警告:
I try to cast an object to my Action class, but it results in a warning:
Type safety: Unchecked cast from Object to Action<ClientInterface>
Action<ClientInterface> action = null;
try {
Object o = c.newInstance();
if (o instanceof Action<?>) {
action = (Action<ClientInterface>) o;
} else {
// TODO 2 Auto-generated catch block
throw new InstantiationException();
}
[...]
感谢您的帮助
Thank you for any help
推荐答案
是的 - 这是 type erasure 。如果 o
实际上是不会被演员捕获的 Action< String>
只有当你尝试使用它,传递一个 ClientInterface
而不是一个字符串。
Yes - this is a natural consequence of type erasure. If o
is actually an instance of Action<String>
that won't be caught by the cast - you'll only see the problem when you try to use it, passing in a ClientInterface
instead of a string.
使用以下命令移除警告:
You can get rid of the warning using:
@SuppressWarnings("unchecked")
作为函数注释,但是你不能很容易地解决基本问题:(
as a function annotation, but you can't easily sort out the underlying problem :(
这篇关于类型安全:从对象取消选中投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文