Generics and use of 'instanceof'
A question specific to generics in Java, and their usage with instanceof
:
For the following if-statement: if (((NodeInternal<Value>) p开发者_JS百科arent).NW() instanceof NodeLeaf<?>)
I need this cast in order to access the parent
's NW
. NW
is a property of parent
which is of type NodeInternal
.
I need to check if the reference pointer held by NW
is an instance of NodeLeaf<?>
. A runtime error is produced: NodeLeaf cannot be cast to NodeInternal
. This is because leaf is cast as an internal, however, the cast should be only for parent
to be able to access NW()
.
How would I fix such a problem?
Thank you
Well, I guess I commented first so I'll do the honors :)
The explanation is that parent
is in fact of class NodeLeaf
and not NodeInternal
.
精彩评论