Java 如何将泛型与类数组一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:02:10  来源:igfitidea点击:

How do I use generics with an array of Classes?

javagenerics

提问by skiphoppy

I want to create an array of Classes, each representing a type that is available in the system I'm building. All the Classes involved are subclasses of a common superclass. So I'd like to do:

我想创建一个类数组,每个类代表我正在构建的系统中可用的类型。所有涉及的类都是一个公共超类的子类。所以我想做:

Class<? extends SuperClass>[] availableTypes = { SubClass1.class, SubClass2.class };

This gives me the error:

这给了我错误:

Cannot create a generic array of Class<? extends SuperClass>.

I get the same message if I try to qualify the creation of the array on the right hand side of the initialization:

如果我尝试在初始化的右侧限定数组的创建,我会收到相同的消息:

Class<? extends SuperClass>[] availableTypes = Class<? extends SuperClass>[] { SubClass1.class, SubClass2.class };

I can get the code to compile if I eliminate the generics qualifications:

如果我消除泛型限定,我可以编译代码:

Class[] availableTypes = { SubClass1.class, SubClass2.class };

But then I get the generics warning:

但是后来我收到了泛型警告:

Class is a raw type. References to generic type Class should be parameterized.

类是原始类型。对泛型类型 Class 的引用应该被参数化。

I'm trying; I'm trying! :) Also, at this point, even if this didn't provoke a warning, I lose a piece of the interface I was trying to define. I don't want to just return an array of arbitrary classes; I want to return an array of classes that are all subclasses of a particular SuperClass!

我想; 我想!:) 另外,在这一点上,即使这没有引起警告,我也会丢失我试图定义的接口的一部分。我不想只返回一组任意类;我想返回一个类的数组,这些类都是特定 SuperClass 的所有子类!

Eclipse has some pretty powerful tools for figuring out what parameters to use to fix generics declarations, but in this case it falls down, as it tends to do when you deal with Class. The "Infer Generic Type Arguments" procedure it offers doesn't change the code at all, leaving the warning.

Eclipse 有一些非常强大的工具来确定使用哪些参数来修复泛型声明,但在这种情况下它会失败,因为它在处理 Class 时往往会这样做。它提供的“推断通用类型参数”过程根本不会更改代码,只会留下警告。

I was able to work around this by using a Collection instead:

我可以通过使用 Collection 来解决这个问题:

List<Class<? extends SuperClass>> availableTypes = new List<Class<? extends SuperClass>>();

But what's the right way to do this with arrays?

但是用数组做到这一点的正确方法是什么?

回答by Simon Nickerson

It seems a bit defeatist, but problems like this are precisely the reason why most people avoid mixing arrays and generics. Because of the way generics are implemented (type erasure), arrays and generics will never work well together.

这似乎有点失败主义,但像这样的问题正是大多数人避免混合使用数组和泛型的原因。由于泛型的实现方式(类型擦除),数组和泛型永远不会很好地协同工作。

Two workarounds:

两种解决方法:

  • Stick to using a collection (e.g. ArrayList<Class<? extends SuperClass>>), which works just as well as an array and also allows expansion.
  • Put a @SuppressWarnings("unchecked")annotation on the code creating the array along with a comment justifying its use.
  • 坚持使用集合(例如ArrayList<Class<? extends SuperClass>>),它与数组一样有效,并且还允许扩展。
  • @SuppressWarnings("unchecked")注释中的代码创建与评论证明其使用沿着阵列。

回答by Michael Myers

The problem is that it is illegal to create an array of a generic type. The only way to get around it is by casting to the generic type when you create the array, but that's not a very good solution. (Note that it is possible to usea generic array, just not create one: see this question.)

问题是创建泛型数组是非法的。解决它的唯一方法是在创建数组时强制转换为泛型类型,但这不是一个很好的解决方案。(请注意,可以使用通用数组,而不是创建一个:请参阅此问题。)

You should almost always be using lists instead of arrays anyway, so I think you came up with the best solution already.

无论如何,您几乎总是应该使用列表而不是数组,所以我认为您已经想出了最好的解决方案。

回答by erickson

Use this syntax:

使用以下语法:

Class<? extends SuperClass>[] avail = new Class[] { SubClass1.class, ... };

It will give you an "unchecked" warning, and rightly so, since you might be including a Classobject for a type that doesn't extend SuperClassin the array.

它会给你一个“未经检查”的警告,这是正确的,因为你可能包含一个Class不在SuperClass数组中扩展的类型的对象。

回答by Eddie

The right way to do this with arrays is to do it with a Collection. Sorry! For a complicated set of reasons, arrays don't play nice with generics. Arrays have a different covariance model than generic objects do, which ultimately causes the problems you are running into. For example, with arrays, but not (normally) with generic objects, you can legally do this:

使用数组执行此操作的正确方法是使用集合执行此操作。对不起!由于一系列复杂的原因,数组不能很好地与泛型搭配使用。数组具有与通用对象不同的协方差模型,这最终会导致您遇到的问题。例如,对于数组,而不是(通常)对于通用对象,您可以合法地这样做:

Object[] myArray = new String[5];

while you cannot do this:

虽然你不能这样做:

LinkedList<Object> myCollection = new LinkedList<String>();

If you want more detail, you can see the Arrays In Java Genericspage from the excellent Generics FAQ

如果您想了解更多详细信息,可以从优秀的泛型常见问题解答中查看Java 泛型中数组页面

As simonn said, you can also just use your arrays as they are, and use @SuppressWarnings("unchecked")to silence the warnings. This will function, but without the type safety that generics can provide you. If it's performance that you are worried about, just use an ArrayListso you are just using a thin wrapper around an array, but with all of the type safety guarantees provided by generics.

正如 simonn 所说,您也可以按原样使用数组,并用于@SuppressWarnings("unchecked")使警告静音。这将起作用,但没有泛型可以为您提供的类型安全。如果您担心的是性能,只需使用 an 即可,ArrayList因此您只是在数组周围使用薄包装器,但具有泛型提供的所有类型安全保证。

回答by John Feminella

But what's the right way to do this with arrays?

但是用数组做到这一点的正确方法是什么?

There's no type-safe way to do this; using the collection is the right approach.To see why, imagine if this were allowed. You could have a situation like this:

没有类型安全的方法可以做到这一点;使用集合是正确的方法。要了解原因,请想象一下是否允许这样做。你可能会遇到这样的情况:

// Illegal!
Object[] baskets = new FruitBasket<? extends Citrus>[10];

// This is okay.
baskets[0] = new FruitBasket<Lemon>();

// Danger! This should fail, but the type system will let it through.
baskets[0] = new FruitBasket<Potato>();

The type system needs to detect whether a basket that gets added to the array is of the type FruitBasket<? extends Citrus>or a subtype. A FruitBasket does not match and should be rejected with an ArrayStoreException. But nothing happens!

类型系统需要检测添加到数组中的篮子是该类型FruitBasket<? extends Citrus>还是子类型。FruitBasket 不匹配,应该被拒绝ArrayStoreException。但什么也没有发生!

Because of type erasure, the JVM can only see the runtime type of the array. At runtime, we need to compare the array type to the element type to make sure they match. The array's runtime component type is FruitBasket[]after type erasure; likewise, the element's runtime type is FruitBasket. No problems will be detected -- and that's why this is dangerous.

由于类型擦除,JVM 只能看到数组的运行时类型。在运行时,我们需要将数组类型与元素类型进行比较以确保它们匹配。数组的运行时组件类型FruitBasket[]在类型擦除之后;同样,元素的运行时类型是FruitBasket. 不会检测到任何问题——这就是为什么这很危险。

回答by Sam Mesh

Not type safe and with unchecked cast warning:

类型不安全且带有未经检查的强制转换警告:

Class<? extends SuperClass>[] availableTypes =
    (Class<? extends SuperClass>[])
    (new Class[]{ SubClass1.class, SubClass2.class });