【发布时间】:2012-12-07 11:23:15
【问题描述】:
我遇到了一个问题,我正在尝试实施“两级”强制转换。
下面是显示我正在尝试做的简化代码:
public class Array2D<T>
{
private T[][] _array;
....
public T get( int x , int y )
....
public void set( T o , int x , int y )
}
直到那里,没问题。
我正在尝试扩展这个类,例如我可以在 getter 和 setter 中封装 SoftReferences 的使用:
public class Array2DSoftRefs<T> extends Array2D<SoftReference<T>>
{
public T get( int x , int y )
{
return super.get(x,y).get(); // get the array element, then the SoftReference contents
}
....
public void set( T o , int x , int y )
{
super.set( new SoftReference<T>(o) ,x,y); // generate the SoftReference on-the-fly
}
}
确实,因为编译器/语法分析器跳过了泛型擦除,所以我被踢了,然后@Override 注释无法帮助我(队长明显)。
我不知道如何从 SoftReference<T> 模板返回 T 类型。
我尝试为SoftReference<T> 放入两个泛型T 和U,但没有成功。
【问题讨论】:
-
Java 中不使用术语模板。 generics 和 type parameters 被使用。
-
确切的编译器错误信息是什么?
-
@jahroy 感谢您的编辑和这个词,我仍然怀念我的 C++ 背景。
-
@PaulBellora 感谢修复。
标签: java generics derived-class type-erasure overriding