问一个generic的问题吧# Java - 爪哇娇娃
T*e
1 楼
该怎么样cast 一个generic的对象啊?
比方说:
SortedSet s0 = new TreeSet();
Object obj o = s0;
...
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
但是这样cast会导致warning:
Type safety: The cast from Object to SortedSet is actually checking
against the erased type SortedSet.
如果:
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
则在add的时候会有如下的warning:
Type safety: The method add(Object) belongs to the raw type Set. References to
generic type Set should be par
比方说:
SortedSet
Object obj o = s0;
...
SortedSet
s1.add( new Integer(1) );
但是这样cast会导致warning:
Type safety: The cast from Object to SortedSet
against the erased type SortedSet.
如果:
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
则在add的时候会有如下的warning:
Type safety: The method add(Object) belongs to the raw type Set. References to
generic type Set