java:如何修复未选中的强制转换警告 [英] java: How to fix the Unchecked cast warning

查看:566
本文介绍了java:如何修复未选中的强制转换警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

private HashMap<Class<?>, HashMap<Entity, ? extends Component>> m_componentStores;

public <T extends Component> T getComponent(Entity e, Class<T> exampleClass)
{
    HashMap<Entity, ? extends Component> store = m_componentStores.get(exampleClass);

    T result = (T)store.get(e);

    if (result == null)
    {
        throw new IllegalArgumentException( "GET FAIL: "+e+" does not possess Component of class\nmissing: "+exampleClass );
    }

    return result;
}



当编译时,它显示 T result = (T)store.get(e)具有未选中的投射。

When I compile, it shows that T result = (T)store.get(e) has an unchecked cast.

Type safety: Unchecked cast from capture#2-of ? extends Component to T

我缺少什么来防止此警告出现?

What am I missing to prevent this warning from appearing?

推荐答案

Class.cast 是你想要的。

更改行:

T result = (T)store.get(e);

到:

T result = exampleClass.cast(store.get(e));

这篇关于java:如何修复未选中的强制转换警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆