[英]Java: extend from a generic class with a specified type
I'm trying to extend a generic class, like: 我正在尝试扩展通用类,例如:
public class GenericClass<T> implements GenericClassInterface<T>
{
public T myMethod(String typeID) {
T test = _get_test_value_; // here I use jackson to retrieve T de-serialized from JSON
return T;
}
}
Then I extend that class: 然后我扩展该类:
public class SpecificClass extends GenericClass<CustomType> implements SpecificClassInterface
{
public CustomType getIstance(String typeID) {
return super.myMethod(typeID);
}
}
The code compiles correctly, but when I debug I find out that type T
, when myMethod is called from SpecificClass, is Object
, and not CustomType
, even if I specified CustomType
when I extended GenericClass
in SpecificClass
. 代码可以正确编译,但是当我调试时,即使我在SpecificClass
扩展GenericClass
时指定了CustomType
,从CustomType
调用myMethod时,类型T
是Object
而不是CustomType
。
How come? 怎么会? Is this a limitation of generics in Java? 这是Java中泛型的限制吗?
The type parameter T
is not available in runtime (during debugging). 类型参数T
在运行时(调试期间)不可用。 This is indeed a limitation of generics in Java and referred to as type erasure. 这确实是Java中泛型的局限性,称为类型擦除。 (All type parameters are transformed to Object
as you have observed.) (正如您所观察到的那样,所有类型参数都转换为Object
。)
See for instance 例如看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.