小编use*_*188的帖子

检查Generic对象的对象类型的正确方法是什么?

编译代码时,我收到了关于Object Casting的警告消息.我不知道如何使用我目前的知识修复它....假设我有一个通用对象MyGenericObj<T>它是从非通用对象扩展的MyObj

这是一个示例代码:

MyObj obj1 = new MyGenericObj<Integer>();
if (obj1 instanceof MyGenericObj) {
    //I was trying to check if it's instance of MyGenericObj<Integer>
    //but my IDE saying this is wrong syntax.... 
    MyGenericObj<Integer> obj2 = (MyGenericObj<Integer>) obj1;
    //This line of code will cause a warning message when compiling 
}
Run Code Online (Sandbox Code Playgroud)

能不能让我知道这样做的正确方法是什么?

任何帮助表示赞赏.

java warnings casting object instanceof

5
推荐指数
1
解决办法
82
查看次数

标签 统计

casting ×1

instanceof ×1

java ×1

object ×1

warnings ×1