泛型类的枚举,数值 [英] generic class of enum, number of values

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

问题描述

如何找出我的枚举在这个例子中有多少值:

How do I find out, how many values my enum has in this example:

public class Analyser<C extends Enum<C>>{
  private long[] dist;
  public Analyser() {
    super();
    dist = new long [C.getEnumConstants().length];
  }
}

最后一行不起作用。

推荐答案

您需要传入枚举的类文字:

You need to pass in the class literal of the enum:

public Analyser(Class<C> enumType) {
    super();
    dist = new long [enumType.getEnumConstants().length];
}

...

Analyser<MyEnum> analyser = new Analyser(MyEnum.class);

这是因为 C 没有意义由于类型擦除,运行时间

This is because C has no meaning at runtime due to type erasure.

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

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