开发者

Generic class to derive from its generic type parameter

开发者 https://www.devze.com 2023-03-21 21:38 出处:网络
Compiles: public class SerializableObject<T> implements Serializable { public T m_object; } Does开发者_高级运维 NOT compile:

Compiles:

public class SerializableObject<T> implements Serializable
{
 public T m_object;
}

Does开发者_高级运维 NOT compile:

public class SerializableObject<T> extends T implements Serializable
{
}

So, I want a generic class to derive from its generic type parameter.

Why?

Let's say I have a Map<K, V> and I simply want to serialize it.

I also don't know ahead which keys I'll have.

How do I do that?


So, I want a generic class to derive from its generic type parameter.

You just can't do that, I'm afraid. There are various technical reasons for this, not least of which is type erasure.

You should look for an alternative solution to your issues - this idea is a dead end.


Nope, your goal is near a Dynamic type, which java does not support.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号