Java 将带有参数化枚举的泛型类作为参数传递给另一个泛型对象
我正在尝试使用枚举作为类型参数在 Java 中实现参数化类。一切工作正常,除了,如果你看下面的代码,有一个带有参数 K
的匿名类 Car.Creator
。当然,应该有 CarObject
,而不是 K
,但这并不那么容易。如果我将 CarObject
放在 K
的位置,则会出现语法错误。有人可以解释一下这样的事情是否可能,并可能提供一些代码示例。
public class CarObject<T extends Enum<T>>
{
public Map<T, String> values;
private Class<T> typeClass;
public CarObject(Class<T> clazz)
{
typeClass = clazz;
values = new EnumMap<T,String>(typeClass);
}
public static final Car.Creator<K> Creator = new Car.Creator<K>()
{
public K create()
{
return new K();
}
public K[] newArray(int size)
{
return new K[size];
}
}
}
我可以给你一个来自官方 Android 文档的示例(查看代码在“类概述”中),这工作得很好。我认为 Android 背后隐藏着一些魔法。我正在尝试做完全相同的事情 - 实现 Parcelable 接口。我只是编造了这个例子,没有实现 Parcelable 和其他东西,因为我认为它可能只是一个语法糖:)。
I am trying to implement parameterized class in Java with enum as a type parameter. Everything works fine, except, if you look at the code below, there is that anonymous class Car.Creator
with parameter K
. But of course, instead of K
, there should be CarObject<T>
, but it's not that easy. If I put CarObject<T>
in K
's place, then I got syntax error. Could someone explain if something like this is possible and maybe provide with some code sample.
public class CarObject<T extends Enum<T>>
{
public Map<T, String> values;
private Class<T> typeClass;
public CarObject(Class<T> clazz)
{
typeClass = clazz;
values = new EnumMap<T,String>(typeClass);
}
public static final Car.Creator<K> Creator = new Car.Creator<K>()
{
public K create()
{
return new K();
}
public K[] newArray(int size)
{
return new K[size];
}
}
}
I can give you an example from official Android documentation(look at the code in 'Class Overview') where this works perfectly fine. I think there is some magic going on under the hood in Android. I'm trying to do exactly the same thing - implement Parcelable interface. I just made up this example without implements Parcelable
and other stuff, because I though maybe it's just a syntax sugar :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下下面的语句 -
看起来你有一个通用类Creator定义为类Car中的静态内部类。并且,在这里您尝试使用它并实例化它,因此不能在这里使用 K ,除非它是在某处定义的类或接口。
我认为下面的例子解释了我的意思 -
Look at the following statement -
Looks like you have a generic class Creator defined as static inner class in the class Car. And, here your trying to use it and instantiate it, so can not use K here unless it's a class or an interface defined somewhere.
I think the following example explains what I mean -
根据编译器消息使该方法成为非静态的,但无论如何您还有其他几个主要的设计问题。 create() 需要来自某处的 clazz,并且您不能返回通用数组。
编辑我通常不会发布代码解决方案,但你似乎很困惑。
Make the method non-static as per the compiler message, but you have several other major design problems anyway. create() needs a clazz from somewhere, and you can't return generic arrays.
EDIT I don't normally post code solutions but you seem pretty confused.
我不是 Android 专家,但我很确定 Android 中发生这种情况并没有什么神奇之处。我认为您只是对泛型类型参数和静态之间的关系感到困惑。
通用类型参数(如示例中的
T
)属于类的实例,而不是类本身。静态成员(如示例中的
Creator
)属于类本身,而不属于类的实例。事实上,
Creator
是一个实现通用接口的匿名内部类,这一事实在这里有点转移注意力。问题很简单,无论Creator
是什么,只要它是static
,它就无法访问类型T。
I'm no Android expert, but I'm pretty sure there's nothing magic about how this is happening in Android. I think you're just confused about the relationship between generic type arguments and statics.
Generic type arguments (like
T
in your example) belong to the instances of the class, not to the class itself.Static members (like
Creator
in your example) belong to the class itself, not to the instances of the class.The fact that
Creator
is an anonymous inner class implementing a generic interface is a bit of a red herring here. The issue is simply that, regardless of whatCreator
is, as long as it isstatic
, it can't access the typeT
.