泛型和'instanceof'的使用

时间:2011-10-10 01:14:12

标签: java generics instanceof

特定于Java中泛型的问题及其在instanceof中的用法:

对于以下if语句:if (((NodeInternal<Value>) parent).NW() instanceof NodeLeaf<?>)

我需要此演员才能访问parent的{​​{1}}。 NWNW的属性,其类型为parent

我需要检查NodeInternal所持有的引用指针是否是NW的实例。产生运行时错误:NodeLeaf<?>。这是因为leaf被强制转换为内部,但是,强制转换应该只有NodeLeaf cannot be cast to NodeInternal能够访问parent

我该如何解决这个问题?

谢谢

1 个答案:

答案 0 :(得分:0)

嗯,我想我先评论过,所以我会做荣誉:)

解释是parent实际上属于班级NodeLeaf,而不是NodeInternal