使用类实例作为泛型类型 [英] Use class instance as generic type

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

问题描述

是否可以使用 SomeClass.class 作为类型的泛型?

Is there any way of using SomeClass.class as the generic type for a type?

例如我有一个枚举:

enum MyEnum {
    FOO(String.class);
    private Class fooClass;
    private MyEnum(Class fooClass) {
        this.fooClass = fooClass;
    }
    public Class getFooClass(){
        return fooClass;
    }
}

然后在我的代码中的某处:

And then somewhere in my code:

List<MyEnum.FOO.getFooClass()> list = new ArrayList<>();


推荐答案

否。
泛型将在编译时求值,而此调用的值如下:

No. Generics are evaluated at compile time while the value of this call:

MyEnum.FOO.getFooClass()

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

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