我如何使用类的数组泛型? [英] How do I use generics with an array of Classes?

查看:74
本文介绍了我如何使用类的数组泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这给了我错误:

 无法创建Class的通用数组<< ;?扩展SuperClass> ;. 

如果我尝试限定右侧数组的创建初始化:

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

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

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

但是我得到了泛型警告:

类是一种原始类型。



我正在尝试;我尝试着! :)此外,在这一点上,即使这不会引发警告,我也会失去一块我正在尝试定义的界面。我不想只返回一个任意类的数组;我想返回一个类的数组,这些类都是特定SuperClass的所有子类!



Eclipse有一些非常强大的工具来确定用来修正泛型声明的参数,但在这种情况下,它会下降,因为它在处理Class时倾向于这样做。它提供的推断泛型类型参数过程根本不会改变代码,并留下警告。



我能够通过使用Collection来解决此问题:

 列表< Class< ;?扩展SuperClass>> availableTypes =新列表< Class< ;?扩展SuperClass>>(); 

但是用数组来做这件事的正确方法是什么?

这似乎有点失败,但像这样的问题正是大多数人避免混合数组和泛型的原因。由于泛型的实现方式(键入擦除) ,数组和泛型将永远无法一起工作。



两种解决方法:


  • 坚持使用一个集合(例如 ArrayList< Class<?extends SuperClass>>< / code>),它可以像数组一样工作,并且允许扩展。 b $ b
  • 在创建数组的代码上添加一个 @SuppressWarnings(unchecked)注释以及评论其用途。


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.

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!

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.

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

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

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

解决方案

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.

这篇关于我如何使用类的数组泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆